Thursday, December 25, 2014

Typecasting

6:08 AM - By azeheruddin khan 0

Typecasting is of two types: -


Implicit Conversion: - this type of conversion involves converting a value of one value type whose size is less than that of the variable of another value type to which it is converted.

For ex: - converting an integer type to long type


using System;
namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 12345;
            long num1 = num; // implicit conversion, indirectly we are converting a integer value to long. 
            Console.WriteLine(num1);
            Console.ReadLine();
        }
    }
}

Output:12345


Explicit Conversion: - this type of conversion also called as cast involves directly converting a variable of one type to another type at the risk of losing information. By performing a cast you are telling the compiler to make the conversion though you may loss data as shown below.



using System;
namespace DataType
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = 12345.7d;
            int a = (int)d;  // when a double value is converted to int, it loses it precision. 
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
}



Output:12345






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