In .NET 4 beta 2, there is the new Numerics namespace with struct BigInteger
. The documentation states that it is an immutable type, as I would have expected.
But I'm a little confused by the post-increment operator (++
). This defintely seems to mutate the value. The following while-loop works:
static BigInteger Factorial(BigInteger n)
{
BigInteger result = BigInteger.One;
BigInteger b = BigInteger.One;
while (b <= n)
{
result = result * b;
b++; // immutable ?
}
return result;
}
This is what MSDN has to say about the Increment operator:
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.
All well and fine, I would have understood if I had to use b = b++
but apparently ++
by itself is enough to change a value.
Any thoughts?
Edit:
As Lasse points out, there is a step-by-step specification for how post-increment works. But this still seems att odds with immutability. For instance, I can't imagine that using this operator is thread-safe.