但是我有点让后期增量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
原文地址:https://www.jb51.cc/csharp/94269.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。