C#Struct使用提示? [英] C# Struct usage tips?
本文介绍了C#Struct使用提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢使用结构很多。
所以在阅读这个文章,有什么其他问题我应该反对使用它们吗?
So after reading this article, are there any other concerns I should have against using them all the time?
- 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
andGetHashCode
methods should be overriden for better performance - implementing the
IEquatable<T>
interface is recommended - redefining and
==
and the!=
operators is also recommended
这篇关于C#Struct使用提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文