SlideShare uma empresa Scribd logo
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 ?

Mais conteúdo relacionado

Mais procurados

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

Mais procurados (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
 

Semelhante a 2013 11 CSharp Tutorial Struct and Class

Semelhante a 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 (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
 
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
 
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
 

Mais 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
 

Último

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
ashishpaul799
 

Último (20)

Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 

2013 11 CSharp Tutorial Struct and Class