加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – BigInteger是否不可变?

发布时间:2020-12-15 06:54:16 所属栏目:百科 来源:网络整理
导读:在.NET 4 beta 2中,有新的Numerics命名空间与struct BigInteger. documentation说,这是一个不可改变的类型,正如我所料. 但是我有点让后期增量operator()感到困惑.这确实似乎使价值变异.以下while循环工作: static BigInteger Factorial(BigInteger n){ BigI
在.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读