Thursday, December 25, 2014

C# Data types

6:07 AM - By azeheruddin khan 0

Before understanding about data types we need to know about what is a variable? A variable is a name given to a memory space which holds a value. For ex: - int a=10, here ‘a’ is a variable of type: Integer, used as a name of a memory space which holds the value: 10. The variable ‘a’ is of integer data type.

These data types are divided into two types: -

  1.  Value types: - The variables to which a value can be directly assigned to are value type variables. All value type data are stored on stack. All value types are derived from System.ValueType. For ex: - the value type: decimal is derived from System.Decimal. Where Decimal is a class belonging to System namespace.
There are 13 value types which are as follows: -

Sbyte: - A variable of type: sbyte can hold 8-bit signed integer which can be either positive or negative. The smallest possible value for a sbyte variable is  -128 and the largest possible value is 127.

 Example program for sbyte

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            sbyte a = 128;
            sbyte b = -127;
            sbyte c = -129;

        }
    }
}

Output:




As shown in the above output, the values 128 and -129 are out of range values for sbyte.

Byte: -   A variable of type: byte can hold only unsigned 8-bit positive integer. The smallest value for a byte variable is 0 and the largest value for a byte variable is 255.


 Example program for Byte

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            byte a = 128;
            byte b = 255;
            byte c = 256;
            byte d = -1;
        }
    }
}


Output:




In the above given program, the values 256 and -1 are not within the range of byte i.e. 0 – 255.

Short: - A variable of short type can hold a 16 bit signed positive or negative integer. The smallest possible value for a short integer is -32768 and the largest possible value for a short integer is 32767.

Example program for Short

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            short t1 = 32768;
            short t2 = -32769;
        }
    }
}

Output:




In the above given program, the values 32768 and -32769 are not within the range of short: -32768 – 32767.

ushort: - a variable of ushort can hold only an unsigned positive 16 bit integer. The letter ‘u’ stands for unsigned. The smallest possible value of a ushort variable is 0 and the largest possible value is 65,535.

Example program for ushort

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            ushort a = 65536;
            ushort b = -1;
        }
    }
}


Output:



.



In the above given program, the values 65536 and -1 are not within the range: 0 – 65535.

int: - A variable of type: int can hold a signed 32 bit integer which can be either positive or negative. The smallest possible value of an int variable   is    -2,147,483,648 and the largest possible value is 2,147,483,647. 

Example program for int

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2147483648;
            int b = -2147483649;
        }
    }
}


Output:





In the above given program, the values: 2147483648 and -2147483649 are not within the range: -2147483648 and 2147483647.

uint: - a variable of type: uint can hold only unsigned 32 bit positive integer.  The smallest possible value of a uint variable is 0 and the largest possible value is 4,294,967,295.


Example program for uint

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            uint a = -1;
            int b = 4294967296;
        }
    }
}

Output:




In the above given program, the values: -1 and 429467296 are not within the range: 0 – 4294967295.

long: - a variable of type: long can hold a signed 64 bit integer which can be either positive or negative. The smallest possible value for a long variable is    -9,223,372,036,854,775,808 and the largest possible value is 9,223,372,036,854,775,807.

Example program for long

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            long a = -9223372036854775809;
            int b = 9223372036854775808;
        }
    }
}


Output:




The above given values in the program are not within the range of long.

ulong: - a variable of type: ulong can hold an unsigned 64 bit positive integer. The smallest possible value for a long variable is 0 and the largest possible value is 18,446,744,073,709,551,615.

Char: -  a variable of type: char can hold a 16 bit Unicode character. The smallest possible value is 0 and the largest possible value is 65535.


Example program for char

namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            char a = 'A';
            char b=1;
            char d=' ';
            char d=',';
            char e='12345';
        }
    }
}


Output:




In the above given program, the value ‘12345’ cannot be assigned to the char variable ‘e’ as this variable can accept a single Unicode character as shown in the output below.

Float: - a variable of float type holds a 32 bit signed floating point value which can be either positive or negative. Its range is -3.4 × 1038to +3.4 × 1038 . Any floating point value must have a suffix ‘f’. Its precision is 7 digits.

Example program for float

using System;
namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            float a = 9.5f;
            float b = 6.6f;
            float c = a + b;
            Console.WriteLine("the sum is " + c);
            Console.ReadLine();
        }
    }
}


Output:the sum is 16.1

Double: - a variable of double type holds a 64 bit signed floating point value which can be either positive or negative. Its range is ±5.0 × 10−324 to ±1.7 × 10308. A double point value must have a suffix ‘d’.  Its precision is 15-16 digits.


Example program for double

using System;
namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = 309.5d;
            double b = 306.6d;
            double c = a + b;
            Console.WriteLine("the sum is " + c);
            Console.ReadLine();
        }
    }
}

Output:the sum is 616.1

Decimal: - a variable of double type holds a 128 bit signed floating point value which can be either positive or negative. Its range is ±-7.9 x 1028 to 7.9 x 1028) / (100 to 28). A double point value must have a suffix ‘m’. Its precision is 28-29 significant digits.


Example program for decimal

using System;
namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal a = 30995.5m;
            decimal b = -30698.6m;
            decimal c = a + b;
            Console.WriteLine("the sum is " + c);
            Console.ReadLine();
        }
    }
}


Output:the sum is 296.9

Boolean: - A variable of bool type can hold two possible values i.e. true or false. A Boolean variable is used to check whether an expression gives the desired result or not.


Example program for boolean

using System;
namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            bool a = true;
            int b = 32;
            a = (b % 2 == 0);
            if (a)
            {
                Console.WriteLine(b + " is an even number");
            }
            else
                Console.WriteLine(b + "is an odd number");
            Console.ReadLine();

        }
    }
}




Output:32 is an even number




Tags:
About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top