不可变类VS结构 [英] Immutable class vs struct

查看:111
本文介绍了不可变类VS结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是唯一途径类在C#结构不同(请纠正我,如果我错了):




  • 类变量的引用,而结构变量的值,因此结构的整个价值被复制到分配和参数传递

  • 类变量都存储在堆栈中指向堆内存的指针,而结构变量是存储堆作为值



假设我有一个不变的结构,即结构与不能修改初始化一次场。每次我通过这个结构作为一个参数或使用分配,价值将被复制并存储在堆栈中。



然后,假设我有这样的不可变的结构是一个不可改变类。这个类的一个实例将被创建一次,并且仅参照类将在分配被复制和参数传送



如果对象是可变的,该行为在这两种情况下将是不同的:当一个将改变对象,在第一种情况下的结构的拷贝将被修改,而在第二种情况下,原始对象将被改变。然而,在这两种情况下的对象是不可改变的,因此没有任何区别,这是否实际上是一类或此对象的用户结构。



由于复制引用不是复制结构更便宜,人们为什么要使用一个不变的结构?



此外,由于的可变结构是邪恶,看起来没有理由使用结构的。



我在哪里错了?


解决方案

由于复制引用比复制结构便宜,为什么有人使用一个不变的结构?




这是并非总是如此。复制一个参考将是上一个64位操作系统,它可能比许多结构较大的8个字节。



还要注意的是创作类的很可能更昂贵。创建一个结构往往是在栈上完全做到了(虽然的有许多例外的),这是非常快的。创建一个类需要创建对象的句柄(垃圾收集器),创建堆栈上的参考和跟踪对象的生命周期。这会增加GC的压力,这也有一个真正的成本。



话虽这么说,创建的的不可变的结构很可能不是一个好主意,这就是为什么准则的一部分类和结构之间进行选择建议您始终使用类,如果你的结构将超过16个字节,如果将彩盒等问题,使差。



这虽这么说,我常常依据我的决定更对有问题的类型的使用目的和意义。值类型应该被用来指代单个值(再次,参考指南),并且通常具有比类不同一语义和预期使用。这往往是一样制作类或结构之间的选择时的性能特点一样重要。


The following are the only ways classes are different from structs in C# (please correct me if I'm wrong):

  • Class variables are references, while struct variables are values, therefore the entire value of struct is copied in assignments and parameter passes
  • Class variables are pointers stored on stack that point to the memory on heap, while struct variables are on stored heap as values

Suppose I have an immutable struct, that is struct with fields that cannot be modified once initialized. Each time I pass this struct as a parameter or use in assignments, the value would be copied and stored on stack.

Then suppose I make this immutable struct to be an immutable class. The single instance of this class would be created once, and only the reference to the class would be copied in assignments and parameter passes.

If the object was mutable, the behavior in these two cases would be different: when one would change the object, in the first case the copy of the struct would be modified, while in the second case the original object would be changed. However, in both cases the object is immutable, therefore there is no difference whether this is actually a class or a struct for the user of this object.

Since copying reference is cheaper than copying struct, why would one use an immutable struct?

Also, since mutable structs are evil, it looks like there is no reason to use structs at all.

Where am I wrong?

解决方案

Since copying reference is cheaper than copying struct, why would one use an immutable struct?

This isn't always true. Copying a reference is going to be 8 bytes on a 64bit OS, which is potentially larger than many structs.

Also note that creation of the class is likely more expensive. Creating a struct is often done completely on the stack (though there are many exceptions), which is very fast. Creating a class requires creating the object handle (for the garbage collector), creating the reference on the stack, and tracking the object's lifetime. This can add GC pressure, which also has a real cost.

That being said, creating a large immutable struct is likely not a good idea, which is part of why the Guidelines for choosing between Classes and Structures recommend always using a class if your struct will be more than 16 bytes, if it will be boxed, and other issues that make the difference smaller.

That being said, I often base my decision more on the intended usage and meaning of the type in question. Value types should be used to refer to a single value (again, refer to guidelines), and often have a semantic meaning and expected usage different than classes. This is often just as important as the performance characteristics when making the choice between class or struct.

这篇关于不可变类VS结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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