C#Struct使用提示?

编程入门 行业动态 更新时间:2024-10-13 04:19:13
本文介绍了C#Struct使用提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我喜欢使用结构很多。

所以在阅读这个文章,有什么其他问题我应该反对使用它们吗?

So after reading this article, are there any other concerns I should have against using them all the time?

  • 什么时候应该使用结构体而不是类?
  • 何时在C#中使用struct?
    • When should I use a struct instead of a class?
    • When to use struct in C#?
    • 推荐答案

      您应该对结构进行以下考虑:

      You should make the following considerations about structs:

      • 结构应该是不可变的(可变结构不直观和不可预测)
      • structs 始终具有无法更改的默认(public parameterless)构造函数
      • 结构大小不能超过16字节
      • 等于和 GetHashCode 方法应该被覆盖以获得更好的性能
      • 实现 IEquatable< T> 界面建议
      • 重新定义和 == 和!= 运算符也被推荐
      • structs should be immutable (mutable structs are not intuitive and unpredictable)
      • structs always have a default (public parameterless) constructor that cannot be changed
      • struct size should not exceed 16 bytes
      • the Equals and GetHashCode methods should be overriden for better performance
      • implementing the IEquatable<T> interface is recommended
      • redefining and == and the != operators is also recommended

更多推荐

C#Struct使用提示?

本文发布于:2023-10-08 14:53:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:提示   Struct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!