C#结构体使用技巧? [英] C# Struct usage tips?

查看:94
本文介绍了C#结构体使用技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用结构很多。



所以阅读本的文章,还有没有其他问题,我应该有反对使用他们所有的时间



另请参见:






解决方案

您应该对结构如下考虑:




  • 结构应该是一成不变的(可变的结构是不直观和不可预测的)

  • 结构的总是的有一个不能更改

  • 默认的(公共的无参数)构造
  • 结构尺寸应不超过16个字节

  • 等于的GetHashCode 方法应该重写获得更好的性能

  • 执行 IEquatable< T> 界面建议

  • 重新定义和 == !=还建议运营商


I like using structs a lot.

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

See Also:

解决方案

You should make the following considerations about structs:

  • 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#结构体使用技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆