c# – BigInteger是否不可变?

在.NET 4 beta 2中,有新的Numerics命名空间与struct BigInteger. documentation说,这是一个不可改变的类型,正如我所料.

但是我有点让后期增量operator()感到困惑.这确实似乎使价值变异.以下while循环工作:

static BigInteger Factorial(BigInteger n)
{
    BigInteger result = BigInteger.One;
    BigInteger b = BigInteger.One;

    while (b <= n)
    {
        result = result * b;
        b++;  // immutable ?
    }
    return result;
}

这是MSDN关于Increment操作符的说法:

Because BigInteger objects are
immutable,the Increment operator
creates a new BigInteger object whose
value is one more than the BigInteger
object represented by value.
Therefore,repeated calls to Increment
may be expensive.

一切都很好,我会明白,如果我不得不使用b = b,但显然本身就足以改变一个价值.

有什么想法吗?

编辑:
如Lasse指出的那样,step-by-step specification是如何进行后增量工作的.但这似乎仍然不可改变.例如,我无法想象使用这个操作符是线程安全的.

解决方法

操作符和 – 在正常和操作符方面实施,所以在现实中:
b++;

相当于:

var temp = b;
b = b + 1;
<use temp for the expression where b++ was located>

现在,正如评论的那样,这可能看起来像是破坏了不变性,但并没有.

你应该这样看看这个代码

var temp = b;
b = BigInteger.op_Add(b,1); // constructs a new BigInteger value
<use temp ...>

这将留下内存中的两个对象,原来的BigInteger值和新的,现在由b引用.您可以轻松地检查以下代码会发生什么:

var x = b;
b++;
// Now inspect the contents of x and b,and you'll notice that they differ

所以原来的对象没有改变,所以它不会破坏不变性,并且回答问题的新的部分,这应该是线程安全的.

这是字符串发生的同样的事情:

String s1 = s2;
s2 += "More";
// Now inspect s1 and s2,they will differ

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...