一聚教程网:一个值得你收藏的教程网站

热门教程

C# struct & class Differences

时间:2022-07-02 11:09:03 编辑:袖梨 来源:一聚教程网

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. 

热门栏目