C# struct/class Differences
struct Direct
{
//...
}
class InDirect
{
//...
}
Events are locked?
Exist on stack or heap?
Can cause garbage collection?
Meaning of this?
Always has a default constructor?
Default construction triggers static construction?
Can be null?
Use with the as operator?
Can be locked?
Can have a destructor?
Default field layout?
Can be a volatile field?
Can have synchronized methods?
Can be pointed to?
Can be stackalloc’d?
Can be sizeof’d?
How to initialize fields?
Inheritance differences?
Equals behavior
Events are locked?
Events declared in a class have their += and -= access automatically locked via a lock(this) to make them thread safe (static events are locked on the typeof the class). Events declared in a struct do not have their += and -= access automatically locked. A lock(this) for a struct would not work since you can only lock on a reference type expression.
Exist on stack or heap?
Value type local instances are allocated on the stack. Reference type local instances are allocated on the heap.
Can cause garbage collection?
Creating a struct instance cannot cause a garbage collection (unless the constructor directly or indirectly creates a reference type instance) whereas creating a reference type instance can cause garbage collection.
Meaning of this?
In a class, this is classified as a value, and thus cannot appear on the left hand side of an assignment, or be used as a ref/out parameter. For example:
class Indirect
{
//...
public void Method(Indirect that)
{
RefParameter(ref this); // compile-time error
OutParameter(out this); // compile-time error
this = that; // compile-time error
}
//...
}
In a struct, this is classified as an out parameter in a constructor and as a ref parameter in all other function members. Thus it is possible to modify the entire structure by assigning to this or passing this as a ref/out parameter. For ex