Working with BigInteger in .NET 4.0

One of the interesting feature that is added with .NET 4.0 is the support of BigInteger. Big numbers are needed when a number cannot be held with any of the existing data types available with .NET framework.
Long has the highest value of 9,223,372,036,854,775,807 which is 0x7FFFFFFFFFFFFFFF in hexadecimal. This is the highest value of a variable which you can store in .NET. Or rather you can say UInt64 to store the highest value of 18,446,744,073,709,551,615 if you don’t need negatives. But this is not actually sufficient when dealing with certain situations like dealing with yearly analytical reports, statistical data etc.
BigInteger is a new type(struct) introduced in .NET 4.0 which deals with the situation and can store any value. To use it you need to add System.Numarics.dll.

For instance :

BigInteger binteger = new BigInteger(3824783785434543);
Console.WriteLine(binteger);

Here in the constructor I have specified the value with which the binteger is to be initialized. You can also use assignment operator and any operators with this object.

BigInteger binteger = 3824783785434543;
Console.WriteLine(binteger);
Console.WriteLine(binteger > 32948454);
Console.Read();

You can associate any normal operators with BigInteger class. You can even store decimal values in this class and it works too.
Some Methods that comes handy at times with BitInteger :
• Pow – Represents Power of a value
• Modulus
• Remainder
• Log
• Min
• Max

To know more about it, please refer to BigInteger

Thanks for reading.
Happy Coding.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434