Search This Blog

Friday, December 28, 2012

How many data types in C# (Integral types)


DataTypes In C#
Value Based Datatypes:

Value based datatypes are those which store the data directly into the variables. Value based datatypes can further be classified as simple types, enum-type and struct-type. We follow a different type of classification which is based on the "data-type".

1.Integral types
2.Floating point types
3.Decimal type
4.Bool type
5.Integral types

C# supports 9 different integral types.

Type # Integral type Data Type Default Value Range

1 sbyte 8-bit signed integer 0 -128 to +127
2 byte 8-bit unsigned integer 0 0 to 255
3 short  16-bit signed integer  0  -32,768 to +32,767
4 ushort 16-bit unsigned integer 0 0 to 65535
5 int 32-bit signed integer  0 -2,147,483,648 to +2,147,483,647
6 uint 32-bit unsigned integer 0 0 and 4294967295
7 long 64-bit signed integer 0 -9,223,372,036,854,775,808 to+ 9,223,372,036,854,775,807
8 ulong 64-bit unsigned integer 0 0 to 18446744073709551615
9 char 16-bit unsigned integer  '\x0000' 0 to 65535

// Example that demonstrates integral datatypes in C#

using System;

    class integral_dtypes
    {
        public static void Main(string[] args)
        {
         sbyte sb=-125;
            byte b=125; // try assigning -125 to this variable
            short s = -32225;
            ushort us = 32225; // try assigning -32225 to this variable
            int i = -123456789;
            uint ui = 123456789; //try assigning -123456789
            long l = -123456789;
            ulong ul = 11112222333344445555;
            char c = 'a';

            Console.WriteLine("Signed byte: " + sb);
            Console.WriteLine("Unsigned byte: " + b);
            Console.WriteLine("Signed short: " + s);
            Console.WriteLine("Unsigned short: " + us);
            Console.WriteLine("Integer: " + i);
            Console.WriteLine("Unsigned integer : " + ui);
            Console.WriteLine("Long: " + l);
            Console.WriteLine("Unsigned Long: " + ul);
            Console.WriteLine("Character: " + c);

        }
    }

Output:

   Signed byte: -125
   Unsigned byte:125
   Signed short:-32225
   Unsigned short:32225
   Integer: -123456789
   Unsigned integer :123456789
   Long: -123456789
   Unsigned Long:11112222333344445555
   Character: a

No comments :