Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structs.

Similar presentations


Presentation on theme: "Structs."— Presentation transcript:

1 Structs

2 Structs vs Classes Both are user-defined types
Both can implement multiple interfaces Both can contain Fields, constants, events, arrays Methods, properties, indexers, operators, constructors Both can have Private Fields Both can have Public Properties Both can have constructors Both can have methods System.Int32 is a struct (ex.)

3 Classes and Structs class CPoint { int x, y; ... }
struct SPoint { int x, y; ... } CPoint cp = new CPoint(10, 20); SPoint sp = new SPoint(10, 20); 10 sp 20 cp CPoint 10 20

4 Class Struct Reference type (holds a reference to an object in memory)
Value type (holds value in memory where it is declared) Can inherit from any non-sealed reference type No inheritance (inherits only from System.ValueType) Can have a destructor No destructor Can have user-defined parameterless constructor No user-defined parameterless constructor Destructors:

5 Class Struct Only reference destroyed immediately after scope is lost. Objects are removed by GC Destroyed immediately after scope is lost When copy, only get a copy of reference When copy, a new copy is created Can be sealed or not Implicitly sealed (no one can extend them)

6 C# Structs vs. C++ Structs (very different)
Same as C++ class, but all members are public User-defined value type Can be allocated on the heap, on the stack or as a member (can be used as value or reference) Always allocated on the stack or in-lined as a member field Members are always public Members can be public, internal or private

7 StructDemo struct SimpleStruct { private int id; private string name;
public int ID get {return this.id;} set { this.id = value; } } public string Name get { return this.name; } set { this.name = value; } public SimpleStruct(int id, string name) : this() this.ID = id; this.Name = name; public override string ToString() return "ID= " + ID + ", Name= " + Name;

8 Stack and Heap

9 Destructors (only in classes)
A destructor in C# overrides System.Object.Finalize method. You have to use destructor syntax to do so. Manually overriding Finalize will give you an error message. Basically what you trying to do with your Finalize method declaration is hiding the method of the base class. It will cause the compiler to issue a warning which can be silenced using the new modifier (if it was going to work). The important thing to note here is that you can't both override and declare a new member with identical name at the same time so having both a destructor and a Finalize method will result in an error (but you can, although not recommended, declare a public new void Finalize() method if you're not declaring a destructor). ~Customer() { } No argument Constructor (only in classes)

10 Enums Strongly typed Can specify underlying type
No implicit conversions to/from int Operators: +, -, ++, --, &, |, ^, ~ Can specify underlying type byte, short, int, long enum Color: byte { Red = 1, Green = 2, Blue = 4, Black = 0 }

11 Delegates Object oriented method(function) pointers Multiple receivers
Each delegate has an invocation list Thread-safe + and - operations Foundation for events delegate void MouseEvent(int x, int y); delegate double MyMethod(double x); MyMethod m= new MyMethod(Math.Sin); double x = m(1.0);

12 Delegates A delegate is similar to a pointer to a function C/C++.
Since pointers are undesirable in C# programming the delegate is the proper alternative. A delegate is a prototype for a method. Instantiate a delegate and set it to reference one or more methods to be called when it is invoked.

13 Unified Type System Everything is an object
All types ultimately inherit from object Any piece of data can be stored, transported, and manipulated with no extra work object Stream Hashtable int double MemoryStream FileStream


Download ppt "Structs."

Similar presentations


Ads by Google