SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
C#

Struct & Class
2013,11

by aecho
References
Dot Net Book Zero
http://www.charlespetzold.com/dotnet/
Author, Charles Petzold
Mantra
Struct is Value type; Class is Reference type.
Outlines
The way of allocating memory block.
New operation
Equality about “==“ and Equals().
Struct
The primitive data types
int, short, double, long, …
DateTime struct, Guid struct, and …
custom defined struct types
struct A { …. }
Class
string class.
custom defined class types.
class A { …. }
Stack

int i = 10;
MyClass a = 

new MyClass();
MyClass b = a;

i

10

a

0x01

b

0x01

Heap

0x01

…
Stack

int i = 10;
DateTime now =

i

DateTime.Now;
Guid id =
Guid.NewGuid();

now

id

10
2013/10/31
12:00:00

“382c74c3-721d
-4f34-57657b6
cbc27”
Stack

Heap

string a = “Hello
World”;
int b[] = new int[]

a

0x01

0x01

b

0x02

0x02

“Hello

World”

{1,2,3};
{1, 2, 3}
New operator
New() will call parameterless constructor in
struct or class.
new DateTime(), new Guid(),
new Contact()
New operator
For struct
Default parameterless constructor.
Always have one.
Initialize all fields to 0 or null.
Can’t be customized.
New operator
A struct example.
New operator
For class
Default parameterless constructor.
If no constructor defined, compiler will
create one.
Could be customized.
New operator
•

PointClass

•
•

Customized parameterless
constructor.

RectClass

•

A parameterless constructor will

•

It initialized all field to 0 or null.

be created by compiler.
New operator
•

As for Cat class

•

No parameterless constructor

•

Only Cat(string aName)

will be created.

constructor can be used.
Equality
“==” vs “Equals()”
A == B? A.Equals(B)?
For struct,
A == B,
depend on the implementations of
operators “==” and “!=”.
A.Equals(B),
T
rue, If all fields of A and B are equal.
A == B? A.Equals(B)?
Stack
pt_1
pt_2

Equals() is T
rue, if all fields are
the same in struct.

x = 3


y=5
x=3

y=5
A == B? A.Equals(B)?
For class,
A == B
T
rue, if they point to the same heap memory address.
A.Equals(B)
Depends on the implementations of Equals().
Equals() of System.Object class
Compare memory addresses.
A == B? A.Equals(B)?
Stack
pt_1 0x01
pt_2 0x02
pt_3 0x01

Heap
0x01
0x02

x = 3


y=5
x=3

y=5
Examples

What is b2 ?
== vs Equals()
Conclusions about Equality,
Compare equality of objects.
Equals()
Compare the memory addresses.
== operator
Any question ?

Contenu connexe

Tendances

Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
TAlha MAlik
 

Tendances (20)

Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
Structure in C
Structure in CStructure in C
Structure in C
 
structure
structurestructure
structure
 
Structures
StructuresStructures
Structures
 
Structure in c
Structure in cStructure in c
Structure in c
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
C++Constructors
C++ConstructorsC++Constructors
C++Constructors
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
Dev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented ProgrammingDev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented Programming
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Core Java
Core JavaCore Java
Core Java
 
Structure c
Structure cStructure c
Structure c
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
 
Structure & union
Structure & unionStructure & union
Structure & union
 

Similaire à 2013 11 CSharp Tutorial Struct and Class

Similaire à 2013 11 CSharp Tutorial Struct and Class (20)

Op ps
Op psOp ps
Op ps
 
637225564198396290.pdf
637225564198396290.pdf637225564198396290.pdf
637225564198396290.pdf
 
Basic c#
Basic c#Basic c#
Basic c#
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Functional Scala 2020
Functional Scala 2020Functional Scala 2020
Functional Scala 2020
 
More expressive types for spark with frameless
More expressive types for spark with framelessMore expressive types for spark with frameless
More expressive types for spark with frameless
 
About decision tree induction which helps in learning
About decision tree induction  which helps in learningAbout decision tree induction  which helps in learning
About decision tree induction which helps in learning
 
D1104294GC10_D1104294GC10_D1104294GC10.pptx
D1104294GC10_D1104294GC10_D1104294GC10.pptxD1104294GC10_D1104294GC10_D1104294GC10.pptx
D1104294GC10_D1104294GC10_D1104294GC10.pptx
 
Real-Time Spark: From Interactive Queries to Streaming
Real-Time Spark: From Interactive Queries to StreamingReal-Time Spark: From Interactive Queries to Streaming
Real-Time Spark: From Interactive Queries to Streaming
 
Java R20 - UNIT-5.pdf
Java R20 - UNIT-5.pdfJava R20 - UNIT-5.pdf
Java R20 - UNIT-5.pdf
 
Vision academy classes_bcs_bca_bba_java part_2
Vision academy classes_bcs_bca_bba_java part_2Vision academy classes_bcs_bca_bba_java part_2
Vision academy classes_bcs_bca_bba_java part_2
 
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdfvision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
vision_academy_classes_Bcs_bca_bba_java part_2 (1).pdf
 
Memory efficient programming
Memory efficient programmingMemory efficient programming
Memory efficient programming
 
Java R20 - UNIT-5.docx
Java R20 - UNIT-5.docxJava R20 - UNIT-5.docx
Java R20 - UNIT-5.docx
 
Matching Dirty Data
Matching Dirty DataMatching Dirty Data
Matching Dirty Data
 
NoSQL Endgame DevoxxUA Conference 2020
NoSQL Endgame DevoxxUA Conference 2020NoSQL Endgame DevoxxUA Conference 2020
NoSQL Endgame DevoxxUA Conference 2020
 
Spark Schema For Free with David Szakallas
 Spark Schema For Free with David Szakallas Spark Schema For Free with David Szakallas
Spark Schema For Free with David Szakallas
 
Eugene Burmako
Eugene BurmakoEugene Burmako
Eugene Burmako
 
Kaggle Winning Solution Xgboost algorithm -- Let us learn from its author
Kaggle Winning Solution Xgboost algorithm -- Let us learn from its authorKaggle Winning Solution Xgboost algorithm -- Let us learn from its author
Kaggle Winning Solution Xgboost algorithm -- Let us learn from its author
 
Pittsburgh code and supply
Pittsburgh code and supplyPittsburgh code and supply
Pittsburgh code and supply
 

Plus de Hung-Wei Liu (6)

2015q4_InnerCourse_Presentation
2015q4_InnerCourse_Presentation2015q4_InnerCourse_Presentation
2015q4_InnerCourse_Presentation
 
Optimistic Offline Locking
Optimistic Offline LockingOptimistic Offline Locking
Optimistic Offline Locking
 
Dynamic Programming Languages
Dynamic Programming LanguagesDynamic Programming Languages
Dynamic Programming Languages
 
Coding Style
Coding StyleCoding Style
Coding Style
 
Defensive Programming
Defensive ProgrammingDefensive Programming
Defensive Programming
 
2013 07 Transaction Isolation Level
2013 07 Transaction Isolation Level2013 07 Transaction Isolation Level
2013 07 Transaction Isolation Level
 

Dernier

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 

Dernier (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 

2013 11 CSharp Tutorial Struct and Class