在 C# 中,值类型与引用类型的使用
我的问题是:
- 什么时候应该使用值类型,什么时候应该使用引用类型?
- 一种相对于另一种的优点和缺点是什么?
- 如果到处都使用引用类型怎么办?这有什么坏处吗?
还请讨论每一种的优点和缺点。我也想了解这一点。
My questions are:
- When should we use value types and when reference types?
- What are the advantages and disadvantages of one over other?
- What if one uses reference types everywhere? Is there any harm in it?
Please also discuss advantages and disadvantages of each one. I want to understand that as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该将值类型用于表示值的小型、不可变类型。
永远不要创建可变结构。
对于其他一切,请使用引用类型。
You should use value types for small, immutable types which represent values.
Never make mutable structs.
For everything else, use reference types.
对于没有自己标识的不可变对象使用值类型(1 就是 1),对于其他事物使用引用类型。
Use value types for immutables that do not have an identity of their own (a 1 is a 1), use reference types for other things.
对此似乎有很多困惑,Jon Skeet 在他的书“C# In Depth, 2nd Ed”中很好地澄清了这一点。 (第 2.3 节)。
我个人的方法(可能正确,也可能不正确)是仅使用结构/枚举(值类型)来表示轻量级原子数据结构,我知道我将在某种逻辑或数学运算中经常使用这些结构 - 想想< code>Point 等。
这样我就可以避免垃圾收集性能损失。然而,Jon 在他的书的这一部分中指出,并不能真正保证某些内容是否会进入堆栈,尤其是在新版本的运行时中。
所以我最好的答案是谨慎使用结构之类的东西,并且如果使用的话要非常清楚为什么要使用它们。注意过早的优化。如果你能拿到一本,请阅读乔恩书中的这一部分,因为他很好地阐明了整个主题。
相关:何时使用结构体?
There seems to be a lot of confusion over this, and Jon Skeet does a good job of clearing it up in his book "C# In Depth, 2nd Ed." (section 2.3).
My personal approach, which may or may not be right, is to ONLY use structs/enumerations (value types) to represent lightweight, atomic data structures that I know I'll be using frequently in some kind of logical or mathematical operations - think
Point
, etc.That way I figure I can avoid the garbage collection performance penalty. However, Jon points out in that section of his book that there's no real guarantee, especially in new versions of the runtime, whether something will go on the stack at all.
So my best answer is use things like structs sparingly and be very conscious about why you're using them if you do. Watch out for premature optimization. And read that section in Jon's book if you can get your hands on a copy, because he does a good job of clarifying this whole topic.
Related: When to use struct?
http://www.albahari.com/valuevsreftypes.aspx
这是我在这一点上的参考。我主要使用引用类型。 IE 类而不是结构。经常说的要点是结构应该只用于小块信息。确实取决于具体情况。查看对象浏览器中的 .net 框架应该会有所帮助,您将看到微软人员所做的事情,并且您可以分析他们为什么创建某些类和结构。
http://www.albahari.com/valuevsreftypes.aspx
this is my reference on this point. I mainly use reference types tbh. IE classes and not structs. The main point that often gets said is that structs should only be used for small pieces of information. Really depends on the exact circumstances. Have a look at the .net framework in the object browser that should help, you will see what the microsoft guys have done and you can analyze why they made certain classes and structs.
不可变值类型和不可变引用类型在语义上几乎相同;唯一的区别是引用类型支持引用相等性检查,该检查可能有意义也可能没有意义,并且值类型可以包装在 Nullable(Of T) 中,而引用类型可以隐式为 null。如果一个类型是不可变的,取决于它的使用方式,可能会出于性能原因而倾向于使用结构体或类;结构对于某些操作(几乎所有操作,大小小于 4 字节)更快,而类对于其他一些操作可能更快(特别是对于大于 16 字节的操作)。此外,某些类型的操作对于结构来说基本上是不可能的。
与一些反对者声称的相反,可变结构类型很有用,但有一些警告。如果一个变量保存了对可变类对象的引用,并且执行了某些操作来更改该对象,则该更改将有效地被保存对该对象的引用的所有内容“看到”。如果一个人希望改变一个对象而不干扰其他任何东西,那么他必须知道他拥有对该对象的唯一引用。通常,确保这一点的唯一方法是将对象中的所有数据复制到新的对象实例中,然后对该新实例进行更改。相比之下,如果有一个可变结构,则可以简单地进行任何想要的更改,而无需创建新实例。
可变结构的唯一真正问题是 .net 使用各种抽象来使它们充当统一类型系统的一部分,并且这些抽象可能会导致在逻辑上应该使用原始结构的地方使用结构的副本。这些替换何时发生并不总是显而易见的,并且它们可能会导致令人困惑和错误的行为。
Immutable value types and immutable reference types are semantically all but identical; the only differences are that reference types support reference equality checks that may or may not be meaningful, and that value types may be wrapped in a Nullable(Of T) while reference types are implicitly nullable. If a type is going to be immutable, depending how it will be used, there may be performance reasons to favor a struct or a class; structs are faster for some operations (nearly all operations, for sizes less than four bytes), while classes may bare faster for some others (especially for things larger than 16 bytes). Further, some types of operations are essentially impossible with structs.
Mutable struct types are useful, contrary to what some naysayers claim, but there are some caveats. If one has a variable that holds a reference to a mutable class object, and one does something to change that object, that change will effectively be "seen" by everything that holds a reference to that object. If one wishes to change an object without disturbing anything else, one must know that one holds the only reference to that object. Often times the only way to be sure of this is to copy all the data from the object into a new object instance, and then make the change to that new instance. By contrast, if one has a mutable struct, one can simply make whatever changes one wants without having to create a new instance.
The only real problem with mutable structs is that .net uses various abstractions to make them behave as part of the unified type system, and these abstractions may cause copies of structures to be used in places where the originals logically should be used. It's not always obvious when these substitutions may occur, and they can lead to confusing and erroneous behavior.