分享
 
 
 

Equals behavior?

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

C# struct/class DifferencesEvents 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:

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 example: copy of the field. Always have a default constructor?A struct always has a built-in public default constructor.

always instantiable whereas a class might not be since all its constructors could be private. Default construction triggers static constructor?A structs static constructor is not triggered by calling the structs default constructor. It is for a class.

Can be null?A struct instance cannot be null.

Use with the as operator?A struct type cannot be the right hand side operand of the as operator.

Can be locked?A struct type expression cannot be the operand of a lock statement.

Can have a destructor?A struct cannot have a destructor. A destructor is just an override of object.Finalize in disguise, and structs, being value types, are not subject to garabge collection.

And the CIL for ~Indirect() looks like this:

Default field layout?The default [StructLayout] attribute (which lives in the System.Runtime.InteropServices namespace) for a struct is LayoutKind.Sequential whereas the default StructLayout for a class is LayoutKind.Auto. (And yes, despite its name you can tag a class with the StructLayout attribute.) In other words the CIL for this:

looks like this:

whereas the CIL for this:

looks like this:

Can be a volatile field?You can't declare a user-defined struct type as a volatile field but you can declare a user-defined class type as a volatile field.

Can have synchronized methods?You can't use the [MethodImpl(MethodImplOptions.Synchronized)] attribute on methods of a struct type (if you call the method you get a runtime TypeLoadException) whereas you can use the [MethodImpl(MethodImplOptions.Synchronized)] attribute on methods of a class type.

Can be pointed to?Clause 25.2 of the C# standard defines an unmanaged type as any type that isn't a reference type and doesn't contain reference-type fields at any level of nesting. That is, one of the following:

Any simple value type (11.1.3, eg byte, int, long, double, bool, etc). Any enum type. Any pointer type. Any user-defined struct-type that contains fields of unmanaged types only. You can never take the address of a instance of a type that is not unmanaged (a fixed variable 25.3).

So, in summary, you can never create a pointer to a class type but you sometimes create a pointer to a struct type.

Can be stackalloc'd?You can only use stackalloc on unmanaged types. Hence you can never use stackalloc on class types. For example:

Can be sizeof'd?You can only use sizeof on unmanaged types. Hence you can never use sizeof on class types. For example:

How to initialize fields?The fields of a class have a default initialization to zero/false/null. The fields of a struct have no default value.

You can initialize fields in a class at their point of declaration. For example:

have to be initialized in a constructor. For example: Inheritance differences?

a struct is implicitly sealed, a class isn't. a struct can't be abstract, a class can. a struct can't call : base() in its constructor whereas a class with no explicit base class can. a struct can't extend another class, a class can. a struct can't declare protected members (eg fields, nested types) a class can. a struct can't declare abstract function members, an abstract class can. a struct can't declare virtual function members, a class can. a struct can't declare sealed function members, a class can. a struct can't declare override function members, a class can. The one exception to this rule is that a struct can override the virtual methods of System.Object, viz, Equals(), and GetHashCode(), and ToString().

Equals behavior?classes inherit Object.Equals which implements identity equality whereas structs inherit ValueType.Equals which implements value equality.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有