Structs

Note

(1)Structs do not support inheritance(继承).
(2)There are some differences in the way constructors work for structs. If you do not supply a default constructor, the compiler automatically creates one and initializes the members to its default values.
(3)Structs are really intended to group data items together.
(4)对于Structs是可以的
Dimensions point;
point.Length = 3;
point.Width = 6;
(5)对于Class上述是不可以的,只可如下:
var point = new Dimensions();
point.Length = 3;
point.Width = 6;
原因:Because point would contain an uninitialized reference—an address that points nowhere, so you could not start setting values to its fields. For a struct, however, the variable declaration actually allocates space on the stack for the entire struct, so it’s ready to assign values to.
(6)Positive and Negative:
1)On the positive side, allocating memory for structs is very fast because this takes place inline or on the stack. The same is true when they go out of scope. Structs are cleaned up quickly and don’t need to wait on garbage collection.
2)On the negative side, whenever you pass a struct as a parameter or assign a struct to another struct (as in A = B, where A and B are structs), the full contents of the struct are copied, whereas for a class only the reference is copied. This results in a performance loss that varies according to the size of the struct, emphasizing the fact that structs are really intended for small data structures.

readonly structs(C# 7.2)

It contains only a constructor that changes its members. The properties only contain a get accessor, thus change is not possible.

ref struct(C# 7.2)

Values only were stored in the stack.
Positive:Garbage collection could be reduced.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,183评论 0 10
  • 直肠子为什么不招人喜欢,因为不够虚伪,嘴不甜,不会人前一套,人后一套,不会睁着眼睛说瞎话,不喜欢哄别人开心,总是直...
    2edd223567a8阅读 1,256评论 0 1
  • 语文高考考点细实,知识深广, 考纲又不断更新几乎每年都有一定的考试内容和范围的变动。在这种大背景下,很多语文老师就...
    付雪莉阅读 4,035评论 0 0
  • 昨晚吃饭时,老二多多不愿吃饭,我说老大你喂弟弟吃饭吧,然后老大就乐呵呵的给弟弟喂饭。一口口米饭下去,老二吃...
    育见阅读 5,684评论 0 2

友情链接更多精彩内容