• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
2013 11 CSharp Tutorial Struct and Class

2013 11 CSharp Tutorial Struct and Class







Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    2013 11 CSharp Tutorial Struct and Class 2013 11 CSharp Tutorial Struct and Class Presentation Transcript

    • 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 ?