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操作符的说法:
一切都很好,我会明白,如果我不得不使用b = b,但显然本身就足以改变一个价值. 有什么想法吗? 编辑: 解决方法
操作符和 – 在正常和操作符方面实施,所以在现实中:
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |