BigInteger in C# 4.0

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 .

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 .

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.

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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

2 Responses

  1. Pingback: BigInteger in C# | ProgramInDotnet

Leave a Reply