.NET Framework 4: new BCL features – BigInteger

In my previous blog post about .NET Framework and CLR 4, I mentioned that, apart from performance and other improvements in .NET Framework like DLR (Dynamic Language Runtime) related stuff, Code Contract (Design by Contract – earlier available as separate download from Dev Labs) there has been improvements in .NET 4 BCL (Base Class Library) like

  • System.Numerics namespace
    • BigInteger data type
  • Tuple<T> – used extensively in DLR and Dynamic languages IronPython, IronRuby
  • SortedSet<T>
  • IO related improvements

In this blog post I’ll discuss about the new BigInteger feature – the other two Tuple and SortedSet would be discussed next.


.NET Framework 4 Base Class Library (BCL) has a new structure called BigInteger. There has been a steady demand of BigInteger class in .NET community and within programmers for a long time – the new BigInteger class meets the demand. The class resides in new System.Numerics class – in fact the new Namespace currently (as of Beta 1) has just a single class called BigInteger.

BigInteger Represents an arbitrarily large signed integer. BigInetger is a nonprimitive integral type that supports arbitrarily large integers. Each integral primitive, such as Byte or Int32, includes a MinValue and a MaxValue property, which define the lower bound and upper bound supported by that data type. In contrast, the BigInteger structure has no lower or upper bound, and can contain the value of any integer.

Here’s how to initialize and use BigInetger. BigInteger can be initialized in following two ways –

  • By using new keyword (similar to initializing a reference type).

  // Use new keyword
BigInteger bigInt1 = new BigInteger(999999999999999999);

 ·         You can declare a BigInteger variable and assign it a value just as you would any numeric type, as long as that value is an integral type.

 // Assign BigInteger an integral value.
BigInteger bigInt2 = 999999999999999999;

·         Assign a decimal or floating-point value to a BigInteger object if you cast the value or convert it first.

// Assign decimal or floating-point value to BigInteger object
BigInteger bigInt3 = (BigInteger)999999999.99999999;

·         Call Parse or TryParse methods to convert the string representation of a number to a BigInteger.

// Call Parse or TryParse methods to convert the string representation of a number to a BigInteger

            string positiveString = "91389681247993671255432112000000", negativeString = "-90315837410896312071002088037140000";

            BigInteger posBigInt = 0, negBigInt = 0;



                posBigInt = BigInteger.Parse(positiveString);



            catch (FormatException)


                Console.WriteLine("Unable to convert the string ‘{0}’ to a BigInteger value.", positiveString);



            if (BigInteger.TryParse(negativeString, out negBigInt))



                Console.WriteLine("Unable to convert the string ‘{0}’ to a BigInteger value.", negativeString);

  • Use BigInteger static methods like (Abs, Pow, Log etc) which returns a BigInteger value to initialize a BigInteger.

// Use BigInteger static methods to initiaze BigInteger
BigInteger bigInt4 = BigInteger.Abs(2343243243243243243);
BigInteger bigInt5 = BigInteger.Pow(99999999999999999, 10);

Here’s the code snippet and it’s output –


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s