Introducing: System.Numeric.BigInteger [Inbar Gazit]

Introducing: System.Numeric.BigInteger [Inbar Gazit]

Rate This
  • Comments 26

Update: BigInteger was cut from the .NET 3.5 release.

Have you ever needed a really huge number?  So big it just couldn't fit into any other base-type in the framework?  Well, no problem anymore, just use the new BigInteger type. You can now have the ability to use integers of any arbitrary size, so they can exponentiate until the cows come home... or memory runs out, whichever comes first :-)

So, what's so "Big" about BigInteger?

I saw some people wondering whether we should have called this new type UnboundedInteger instead of BigInteger and I would like to tell you what I think. First, you should know that the BCL team does not take naming lightly. We spend a lot of time and energy making sure we choose the best names for our types, methods, properties etc. I believe that good names are not only technically accurate but also intuitive and easy to remember and use. That's one of the reasons that made us decide to use the name BigInteger. The other reason was that other languages are using the prefix "Big" for unbounded types.  Isn't that huge? Enormous? Gigantic? Well, I think it's simply Big...

Using the new BigInteger

So, what do you need to do in order to use this new type?

First you need to get the latest CTP for the new version of Visual Studio (code name – "Orcas") and install it so that you also get the new version of the .NET Framework including this new type. It would be a good idea to install it on a separate computer or make sure you back-up your important work first. Remember, this is pre-beta software... (in the January CTP you can choose either a Virtual PC image or to use the actual installation bits)

If you do install the bits, all you need is the new System.Core.dll as a reference in your project.  The new BigInteger class is in this new assembly.

From this point on I'm going to be talking C# although this can all work for other languages (such as VB.NET).

Create a new C# project and add a new Using System.Numeric at the top of your cs module. Now, you're ready to use BigInteger in your code.

Here is a sample code to calculate factorials:

using System;

using System.Numeric;


namespace TryOutBigInteger {

    class MyBigIntApp {

        static BigInteger BigFactorial(BigInteger x) {

            if (x < 0)

                return BigFactorial(-x);

            else if (x <= 1)

                return x;

            else return x * BigFactorial(--x);



        static void Main(string[] args) {

            BigInteger x = BigInteger.Parse(Console.ReadLine());






Here is what we get when we run it:






Wow!  I wish that was my bank account balance :-)

BigInteger supports most of the operations that int supports including +, -, *, /, ++, --, %, DivRem GreatestCommonDivisor, Pow, ModPow, Abs and Negate.  All of these are static methods you can find using intellisense by typing "BigInteger".

Bitwise operations are not supported, but we will have an MSDN article coming soon with how to write your own library extension of BigInteger.

Please try the new BigInteger type and let us know what you think. We'd love to hear your feedback!

  • Excellent work on the BigInteger type!  I've been going over the assembly with Reflector, and it looks like it will be very useful.

    One concern, though - the type uses managed arrays as a backing store.  It is likely that many people will implement cryptographic operations using BigInteger, and the potential for sensitive information (keys, etc.) to remain in memory is high.  Is there any plan to mitigate this risk, or am I missing some implementation detail and overthinking here?


  • There's been a need for this for some time, and the guys have finally included this new BCL type in the

  • awesome work! i can't wait to use it.

  • The most important (good) part of the naming decision, IMO? It's the one all Java programmers will look up first when they want to find the type.

    So, can we expect BigDecimal next? :)


  • TrackBack from

  • I ran the code supplied above and gave it 4000 to find the factorial and it worked, but when I gave it 5000 I got a StackOverflowException.  Why is this?  I have over 1GB memory - it seems like it should of worked.  Any thoughts?

  • Sorry, the way I implemented Factorial is using recursion which means that we are going to use the stack before we run out of memory as each time you make another call into Factorial the stack grows and the number of calls is the factorial you are trying to implement. You can write this method in an iterative way using a simple for loop and you would avoid this issue.

  • That's cool and all, but are you going to create an INumeric interface so I can use generics with it?

    You know, something that supports this syntax

    Function Average(Of T AS INumeric)(a as T, b as T)

    Return A.Add(b).Divide(2)

    End Function

  • I changed the recursion to a loop and then computed the factorial for 50,000...the article with the results are here:

  • You've been kicked (a good thing) - Trackback from

  • >Bitwise operations are not supported, but we will have an MSDN article coming soon with how to write your own library extension of BigInteger.

    "Great Idea"

    May be include extension code example from article to BigInteger standart implementation? I can't understand your policy: write article about how inmplement bitwise operations and not implement it in BigInteger.

    Bitwise operation are used often!

  • IMHO BigDecimal and bitwise operations are extremely needed

  • We definitely need the ability to work with numeric types in generic methods.

  • Wow, I am so used to working with Java and C# at the same time I always just assumed that .NET had a BigInteger type already ;)  

    Well, I guess better late than never.  I do wonder what the BCL people were doing when they designed certain parts of the framework initially.  This reminds me of the total absence of a LinkedList type of collection class before 2.0.  How could they forget something like that?  I wanna call "slackers", but other parts of the BCL are so well though out.  Its perplexing.

    On a different note, why does every MS project have a "code name" that has nothing to do with the project or what the project will really eventually be called?  Seems goofy ;)

  • Smalltalk had “LargeInteger” since the 1970s.

    In Smalltalk you do not even need to type your variable to LargeInteger: Any computation with an integer that flows over, will automatically convert to LargeInteger.

Page 1 of 2 (26 items) 12