Sunteți pe pagina 1din 1

Structures Structures are user-defined value type variables, which store their data on the stack.

Structures are similar to classes of C#. They are value types stored on the stack, the advantages compared to class objects stores on the heap are - They are created much more quickly then heap-allocated types. - They are instantly and automatically deallocated once they go out of scope -It is easy to copy value type variables on the stack. struct struct-name struct Student { { data member-1; public string Name; data member-2; public int RollNumber; ---------------public double TotalMarks; } } Student s; s is a variable of type Student and has three member variables. A struct variable is initialized to the default values of its members as soon as it is declared. Difference Between Classes and Structs Category Classes Data type Reference type and therefore stored on the heap Inheritance Support Inheritance Default values Default value of a class type is null Field initialization Permit initialization of instance fields Constructors Permit declaration of parameterless constructors Destructors Supported Assignment Assignment copies the reference Structs Value type and therefore stored on the stack. Do not Support Inheritance Default value is the value produced by zeroing out the fields of the struct Do not permit initialization of instance fields Do not permit declaration of parameterless constructors Not Supported Assignment copies the values.

Enumerations Enumerations are user-defined integer value type variables, which store their data on the stack. The enum keyword automatically enumerates a list of words by assigning values 0,1,2, etc. Eg. enum Shape {Circle, Square, Triangle} Here Circle has the value 0, square 1 and Triangle 2 Example Program Enumerator Initialization enum Shape {Circle = 1, Square = Circle +3, Triangle = Circle + Square + 6} Therefore the value of Circle =1, Square = 4, Triangle = 11. If the first member in declaration of an enum has no initailizer, then its value is set to 0. Enumerator Base types By default, the type of an enum is int. The valid base types are byte, sbyte, short, ushort, int, uint, long and ulong For eg. enum Position : byte { off, on }

S-ar putea să vă placă și