为什么是C#结构不变? [英] Why are C# structs immutable?

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

问题描述

我只是好奇,想知道为什么结构,字符串等是不可变的?是什么使他们不可变对象是可变的和休息的原因。什么是被认为使对象不可改变的事?

I was just curious to know why structs, strings etc are immutable? What is the reason for making them immutable and rest of the objects as mutable. What are the things that are considered to make an object immutable?

有没有在路上有什么区别内存是如何分配和释放的可变和不可变对象?

Is there any difference on the way how memory is allocated and deallocated for mutable and immutable objects?

推荐答案

如果这个主题您的利益,我有一些不可变的编程文章在<一个href="http://blogs.msdn.com/b/ericlippert/archive/tags/immutability/">http://blogs.msdn.com/b/ericlippert/archive/tags/immutability/

If this subject interests you, I have a number of articles about immutable programming at http://blogs.msdn.com/b/ericlippert/archive/tags/immutability/

我只是好奇,想知道为什么结构,字符串等是不可变的?

I was just curious to know why structs, strings etc are immutable?

结构和类都不是一成不变的默认情况下,虽然它是使结构不变的最佳实践。我喜欢一成不变的类了。

Structs and classes are not immutable by default, though it is a best practice to make structs immutable. I like immutable classes too.

字符串是不可变的。

是什么让他们不可变对象是可变的和休息的原因。

What is the reason for making them immutable and rest of the objects as mutable.

  • 这是比较容易推理不改变的对象。如果我有一个有三个项目在里面排队,我知道这是不是现在是空的,它不是空的五分钟前,它不会是空的未来。这是永恒不变的!有一次,我知道这件事的事实,我可以用这个事实永远。不可变对象事实不会变坏。

Reasons to make all types immutable:

  • It is easier to reason about objects that do not change. If I have a queue with three items in it, I know it is not empty now, it was not empty five minutes ago, it will not be empty in the future. It's immutable! Once I know a fact about it, I can use that fact forever. Facts about immutable objects do not go stale.

    第一点的一个特例:不可变对象更容易使线程安全的。大多数线程安全问题都是由于写在一个线程,并读取上的另一个;不可变对象没有写。

    A special case of the first point: immutable objects are much easier to make threadsafe. Most thread safety problems are due to writes on one thread and reads on another; immutable objects don't have writes.

    不可变对象可以拆开并重新使用。例如,如果你有一个不变的二叉树,那么你可以使用它的左,右子树作为一个的不同的的树子树,而不必担心它。在一个可变的结构,你通常最终使数据的副本来重新使用它,因为你不想改变一个逻辑对象影响到另一个。这样可以节省的大量的时间和内存的。

    Immutable objects can be taken apart and re-used. For example, if you have an immutable binary tree then you can use its left and right subtrees as subtrees of a different tree without worrying about it. In a mutable structure you typically end up making copies of data to re-use it because you don't want changes to one logical object affecting another. This can save lots of time and memory.

    有很多原因使结构不变。这里只有一个。

    There are lots of reasons to make structs immutable. Here's just one.

    结构是按值拷贝,而不是引用。这是很容易不小心把一个结构作为复制参考。例如:

    Structs are copied by value, not by reference. It is easy to accidentally treat a struct as being copied by reference. For example:

    void M()
    {
        S s = whatever;
        ... lots of code ...
        s.Mutate();
        ... lots more code ...
        Console.WriteLine(s.Foo);
        ...
    }
    

    现在你想重构一些是code变成一个辅助方法:

    Now you want to refactor some of that code into a helper method:

    void Helper(S s)
    {
        ... lots of code ...
        s.Mutate();
        ... lots more code ...
    }
    

    错了!这应该是(裁判šš) - 如果你不这样做,那么突变会出现第一个上的复制的。如果不允许摆在首位的突变,则所有这些各种各样的问题消失。

    WRONG! That should be (ref S s) -- if you don't do that then the mutation will happen on a copy of s. If you don't allow mutations in the first place then all these sorts of problems go away.

    记住我约约不可改变的结构保持事实事实第一点?

    Remember my first point about facts about immutable structures staying facts?

    假设字符串是可变的:

    public static File OpenFile(string filename)
    {
        if (!HasPermission(filename)) throw new SecurityException();
        return InternalOpenFile(filename);
    }
    

    如果敌对来电变异文件名的的安全检查和的的文件被打开?在code刚刚开业,他们可能没有权限到一个文件!

    What if the hostile caller mutates filename after the security check and before the file is opened? The code just opened a file that they might not have permission to!

    此外,可变数据是很难推理。你要呼叫方有权看到该字符串描述的文件的事实是真实的永远的,不会的直到突变发生的。随着可变字符串,编写安全的code,我们会不断地必须使数据的副本,我们知道不会改变。

    Again, mutable data is hard to reason about. You want the fact "this caller is authorized to see the file described by this string" to be true forever, not until a mutation happens. With mutable strings, to write secure code we'd constantly have to be making copies of data that we know do not change.

    什么是被认为使对象不可改变的事?

    What are the things that are considered to make an object immutable?

    请问型逻辑重新present的东西是一个永恒的价值?号码12为12号;它不会改变。整数应该​​是一成不变的。点(10,30)是点(10,30);它不会改变。点应该是一成不变的。字符串ABC的字符串abc;它不会改变。字符串应该是一成不变的。列表(10,20,30)不发生变化。等等。

    Does the type logically represent something that is an "eternal" value? The number 12 is the number 12; it doesn't change. Integers should be immutable. The point (10, 30) is the point (10, 30); it doesn't change. Points should be immutable. The string "abc" is the string "abc"; it doesn't change. Strings should be immutable. The list (10, 20, 30) doesn't change. And so on.

    有时候型重presents事情做出改变。玛丽·史密斯的姓氏是史密斯,但明天她可能是玛丽·琼斯。还是今天史密斯小姐可能是史密斯博士的明天。外籍人得到了50名健康加分,但现在被击中的激光束后,有十个。有些东西是psented的突变最好再$ P $。

    Sometimes the type represents things that do change. Mary Smith's last name is Smith, but tomorrow she might be Mary Jones. Or Miss Smith today might be Doctor Smith tomorrow. The alien has fifty health points now but has ten after being hit by the laser beam. Some things are best represented as mutations.

    有没有在路上有什么区别内存是如何分配和释放的可变和不可变对象?

    Is there any difference on the way how memory is allocated and deallocated for mutable and immutable objects?

    不是这样。正如我前面提到的,虽然,约不变值的好东西之一是,东西可以再利用它们的部分,而不进行复印。因此,在这个意义上说,内存分配可以是非常不同的。

    Not as such. As I mentioned before though, one of the nice things about immutable values is that something you can re-use parts of them without making copies. So in that sense, memory allocation can be very different.

    这篇关于为什么是C#结构不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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