We will cover following points in this article
1. Syntax of struct and class
2. Difference between Struct and Classes
3. Understanding difference between Stack & Heap
Struct is declared using the struct keyword.
Class is declared using the class keyword
Struct vs Classes
1. Struct is a value type and inherits from System.Value. Class is a reference type and inherits from System.Object.
2. Struct objects are stored on stack. Class objects are stored on the heap.
3. Struct can’t be base type to classes & to other structure. class can inherit another class and it can be base class to another classes. Both Struct and Class can inherit from an interface.
4. Struct can not have default (parameter less) constructor but it can have parameterized constructors. Class can have default (parameter less) as well as parameterized constructors.
5. Struct can not have destructors. But Class can have destructors.
6. Struct can not initialize its member at the time of declaration. Class can initialize its member at the time of declaration.
7. Struct can be instantiated without using the new keyword. The new keyword should be used to create an object of the class.
8. Struct is by default sealed. It cannot use the abstract, sealed and base keyword. Keywords like abstract, sealed and base can be used in context of classes.