BigInteger in C# 4.0

In C# 4.0 Microsoft has added so many features and I love all most all the features. In today’s post we are going to discuss BigInteger Class. During programming some complex systems often we need a very big numbers. For example if we use some of asymmetrical cryptographic feature which require to use large numbers or we can give simple example of factorial where we sometime reached to limit of data type provided by C# compiler. At that time this BigInteger data type can be very handy. You can store 232 to 264 number in this data type. So its very big and you can copy very very big number in that data type.

So let’s take a simple example to write a factorial program. BigInteger data type comes under System.Numerics namespace. So first we have to add reference to our program to System.Numerics assembly like following.

Big Integer In C# 4.0 - Add Reference to System. Numeric

Now we have added the reference to System.Numeric so we are ready for code. Here in code I have just taken a very large number in big integer which is out of range of integer to test out. So I have assigned ‘39242937852522522’ to big integer object and printed that. Following is a code for that.

using System;
using System.Numerics;

namespace ExperimentConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            BigInteger n = 39242937852522522;
            Console.WriteLine(n);
        }
    }
}
Let’s run code and see output like following as expected its printing value.

Output

If you are run same program with integer it will not compile. It will give ‘Can not covert type long to integer error’. That’s it. Hope you liked it. Stay tuned for more.. Till then happy programming. Namaste!!

Shout it

3 comments :

  1. Hi Jaggu,

    I am a fan of yours. While my comment is not related to the post but I am facing difficulties. I want that when user registers as user abc, he can browse my site using abc.host.com. I don't want to create dynamic subdomains but rather give alias to it. I think you have done something like this in one of your projects, please can you prepare some blog post or may be guide me something related to this? Thanks in advance :)

    ReplyDelete
  2. Jaggu, are you not going to respond to my doubts?

    ReplyDelete
  3. Hello @Anonymous

    You can rewrite the URL.

    Best Regards,
    Jalpesh

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.