Cloud Training (Online)




What is BigInteger in C# 4.0 ?

This blog post will explain what is a BigInteger and why we need it in C#.

When talking to one of my friend today , he mentioned about the BigInteger data type in Java . I was wondering what this datatype equivalent be in C# and ended up to see this datatype in C# too .

This is available in C# 4.0 .


What is BigInteger in C# 4.0 ?

The BigInteger represents a large signed integer where its value has no upper or lower bounds so that it can be used on operation on very large values

To use the BigInteger in C# , one might need to add the reference of the System.Numerics assembly to the project .

What is BigInteger in C# 4.0 ?

In the following example , i created a BigInteger with a value that is equivalent to the Power of Int64.MaxValue . ( Int64.MaxValue X  Int64.MaxValue X  Int64.MaxValue ) .

BigInteger MyValue1 = BigInteger.Multiply(Int64.MaxValue, Int64.MaxValue);
MyValue1 = BigInteger.Multiply(MyValue1, Int64.MaxValue);
MessageBox.Show("Value= " + MyValue1.ToString() + "\n" + " Length=" + MyValue1.ToString().Length.ToString());

Just see the resulting output and the length of the variable .. Its is quite BIG.

What is BigInteger in C# 4.0 ?

Note that using BigInteger may cause Out of Memory Exception when its value grow too large


Want to explore more on BigInteger in .NET Framework 4.0 ? Check BigInteger Structure in MSDN . It has a great content about the BigInteger