in Education by
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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The operators ++ and -- are implemented in terms of the normal + and - operators, so in reality: b++; is equivalent to: var temp = b; b = b + 1; Now, as commented, this might seem like it breaks immutability, but it does not. You should instead look at this code as doing this: var temp = b; b = BigInteger.op_Add(b, 1); // constructs a new BigInteger value This will leave two objects in memory, the original BigInteger value, and the new one, now referenced by b. You can easily check that this is what happens with the following code: var x = b; b++; // now inspect the contents of x and b, and you'll notice that they differ So the original object did not change, hence it does not break immutability, and to answer the new part of the question, this should be thread-safe. This is the same thing that happens to strings: String s1 = s2; s2 += "More"; // now inspect s1 and s2, they will differ

Related questions

0 votes
    In .NET 4 beta 2, there is the new Numerics namespace with struct BigInteger. The documentation states that it is an ... = BigInteger.One; BigInteger b = BigInteger.One; while (b...
asked May 8, 2022 in Education by JackTerrance
0 votes
    In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 31, 2022 in Education by JackTerrance
0 votes
    In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 29, 2022 in Education by JackTerrance
0 votes
    In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have a generic method defined like this: public void MyMethod(T myArgument) The first thing I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Lets say I have the option of identifying a code path to take on the basis of a string comparison ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Lets say I have the option of identifying a code path to take on the basis of a string comparison ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I want to convert the matched expression in string or int. But in .NET Framework I do not find ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have a datagridview that I'm using in Visual Studio 2008 for a winforms app. The datagridview is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I have a datagridview that I'm using in Visual Studio 2008 for a winforms app. The datagridview is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I am using C#.Net to send out a HTML email which contains embedded images. These emails work fine ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I am using C#.Net to send out a HTML email which contains embedded images. These emails work fine ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
...