SlideShare a Scribd company 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 ?

More Related Content

DOCX
Structure in c sharp
PPTX
Constructor&method
PPTX
Constructors
PPTX
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
PPT
Structure in c
PPTX
Class object method constructors in java
PPTX
Constructor ppt
PPTX
Lecture 4.2 c++(comlete reference book)
Structure in c sharp
Constructor&method
Constructors
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Structure in c
Class object method constructors in java
Constructor ppt
Lecture 4.2 c++(comlete reference book)

What's hot (20)

PPTX
Chapter2 array of objects
PPTX
Structure in C
PPT
structure
PDF
Structures
PPTX
Constructor and Types of Constructors
PPT
Unit4 C
PPTX
Cs1123 12 structures
PPTX
Structure in c language
PPTX
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
PPTX
Constructor in java
PPTX
C++Constructors
PPTX
C programing -Structure
PDF
CLASSES, STRUCTURE,UNION in C++
PPTX
Dev Concepts: Object-Oriented Programming
PPTX
constructors in java ppt
PPT
Core Java
PPT
Structure c
PPTX
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
PPTX
Structure & union
Chapter2 array of objects
Structure in C
structure
Structures
Constructor and Types of Constructors
Unit4 C
Cs1123 12 structures
Structure in c language
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Constructor in java
C++Constructors
C programing -Structure
CLASSES, STRUCTURE,UNION in C++
Dev Concepts: Object-Oriented Programming
constructors in java ppt
Core Java
Structure c
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
Structure & union
Ad

Similar to 2013 11 CSharp Tutorial Struct and Class (20)

PPT
Introduction to C#
PDF
Visual Programming Lacture Nine 9 Structure.pdf
PPTX
When to use a structure vs classes in c++
PPT
Introduction-to-Csharpppppppppppppppp.ppt
PPT
Learn C# at ASIT
PDF
C# quick ref (bruce 2016)
PPT
Advanced c#
PDF
Introduction to c#
PDF
Introduction To Csharp
PPTX
PDF
C# Structures - Computer Science and Engineering Department
PPT
Introduction to csharp
PPT
Introduction to csharp
PPT
Introduction to csharp
PPT
Learn c sharp at amc square learning
PPT
C# Language Overview Part II
PPT
CSharp_03_ClassesStructs_and_introduction
PPTX
LEARN C# PROGRAMMING WITH GMT
PPT
Synapseindia dot net development
Introduction to C#
Visual Programming Lacture Nine 9 Structure.pdf
When to use a structure vs classes in c++
Introduction-to-Csharpppppppppppppppp.ppt
Learn C# at ASIT
C# quick ref (bruce 2016)
Advanced c#
Introduction to c#
Introduction To Csharp
C# Structures - Computer Science and Engineering Department
Introduction to csharp
Introduction to csharp
Introduction to csharp
Learn c sharp at amc square learning
C# Language Overview Part II
CSharp_03_ClassesStructs_and_introduction
LEARN C# PROGRAMMING WITH GMT
Synapseindia dot net development
Ad

More from Hung-Wei Liu (6)

PDF
2015q4_InnerCourse_Presentation
PDF
Optimistic Offline Locking
PDF
Dynamic Programming Languages
PDF
Coding Style
PPTX
Defensive Programming
PDF
2013 07 Transaction Isolation Level
2015q4_InnerCourse_Presentation
Optimistic Offline Locking
Dynamic Programming Languages
Coding Style
Defensive Programming
2013 07 Transaction Isolation Level

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Pharma ospi slides which help in ospi learning
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pharma ospi slides which help in ospi learning
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
VCE English Exam - Section C Student Revision Booklet
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Presentation on HIE in infants and its manifestations
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial diseases, their pathogenesis and prophylaxis
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Computing-Curriculum for Schools in Ghana
O7-L3 Supply Chain Operations - ICLT Program

2013 11 CSharp Tutorial Struct and Class