|
Post by Admin on Nov 19, 2012 19:15:16 GMT -5
What is the difference between a class and a struct in C#?
>> Struct is a value type. Class is a reference type. >> Struct instances are allocated on the stack (as they are value types). Class instances are allocated on the heap (as they are reference types). >> A struct is implicitly sealed whereas a class is not. >> A struct cannot be abstract, whereas a class can. >> lock does not work on an instance of a struct as it is a value type. lock works on an instance of a class. >> Events declared in struct do not have their += and -= automatically locked. >> Struct always have a default built-in constructor whereas class does not have. >> A class instance can be null whereas a struct instance cannot be null (as it is value type) >> A struct cannot participate in "as" operator. >> A struct does not particpate in Garbage Collection and hence it does not a ~Destructor() concept. >> SizeOf cannot be used on a Class. It can be used on Structs. >> Fields of a struct do not have a default value whereas fields of a class have a null/false/zero default value.
|
|