C Sharp (C#) Unsigned Integer (unit) data type


This is similar to Integer datatype that is explained here : int but the only difference is , it cannot hold any negative values. It can hold only 0 and positive values. So the max number it could hold will be higher than and int datatype. (Limit being : 0 to 4,294,967,295)

Using the same sample as listed in the integer datatype, for bigger number, have used uint instead of int and it works perfectly fine as the limit falls within the range of uint.

While defining an unit datatype , its recommended to use "u" as a suffix to the value thats getting assigned.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 200000000;
            int num2 = 200000000;
            int num3 = num1 + num2;
            Console.WriteLine("The addition of num1 and num2 is : {0}", num3);
            Console.ReadLine();

            //Here the numbers are more than the limit that int variable could hold.
            uint num4 = 2000000001u;
            uint num5 = 2000000003u;
            uint num6 = num4 + num5;
            Console.WriteLine("The addition of num4 and num5 is : {0}", num6);
            Console.ReadLine();           
        }
    }
}

Output :


C Sharp Datatypes Home Page

No comments: