Interview Part 2
Difference between Class and Struct
Class :
- Classes are reference types, allocated on the heap and garbage-collected.
- Can support inheritance
- The reference can be null
- reference type only the variable destroy after the scope is lost.
- classes can have destructors.
- Classes can contain constructor or destructor.
- Class has limitless features.
- Classes used new keyword for creating instances.
Struct :
- Structs are value types, allocated either on the stack or inline in containing types.
- Cannot support inheritance
- Cannot have a null reference
- Value types destroyed immediately after the scope is lost.
- Structs are sealed type.
- A structure couldn't be null like a class.
- Structs can not have destructors,
- Structure does not contain constructor or destructor.
- Struct has limited features.
- Can’t have modifiers like
abstract
,sealed
, andstatic,class
Struct can create an instance, without new keyword.