C Sharp (C#) Short and ushort datatypes


The short and ushort datatypes that are used in C# are similar to int. The only difference between them is the range thats supported. Short supports from –32,768 to 32,767 and ushort supports from 0 to 65,535.

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

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

            //Here the numbers that can be accommodated by ushort but not by short.
            ushort num4 = 20000;
            ushort num5 = 20000;
            ushort num6 = (ushort)(num4 + num5);
            Console.WriteLine("The addition of num4 and num5 is : {0}", num6);
            Console.ReadLine();           
        }
    }
}

Output:

C Sharp Datatypes Home Page

No comments: