Structs Structs vs Classes Both are userdefined types
Structs
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. Int 32 is a struct (ex. )
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); sp 10 20 cp CPoint 10 20
Class Struct Reference type (holds Value type (holds value in a reference to an object memory where it is declared) in memory) Can inherit from any non-sealed reference type No inheritance (inherits only from System. Value. Type) Can have a destructor No destructor Can have user-defined No user-defined parameterless constructor Destructors: http: //msdn. microsoft. com/en-us/library/66 x 5 fx 1 b. aspx
Class Only reference destroyed immediately after scope is lost. Objects are removed by GC When copy, only get a copy of reference Can be sealed or not Struct Destroyed immediately after scope is lost When copy, a new copy is created Implicitly sealed (no one can extend them)
C# Structs vs. C++ Structs (very different) C++ Struct C# Struct 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
Struct. Demo struct Simple. Struct { 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 Simple. Struct(int id, string name) : this() { this. ID = id; this. Name = name; } public override string To. String() { return "ID= " + ID + ", Name= " + Name; } }
Stack and Heap
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)
Enums n Strongly typed o o n No implicit conversions to/from int Operators: +, -, ++, --, &, |, ^, ~ Can specify underlying type o byte, short, int, long enum Color: byte { Red = 1, Green = 2, Blue = 4, Black = 0 }
Delegates n n n Object oriented method(function) pointers Multiple receivers o Each delegate has an invocation list o Thread-safe + and - operations Foundation for events delegate void Mouse. Event(int x, int y); delegate double My. Method(double x); My. Method m= new My. Method(Math. Sin); double x = m(1. 0);
Delegates n n 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.
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 Memory. Stream Hashtable File. Stream int double
- Slides: 13