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.