Search

Sunday, June 15, 2014
0 comments

C# - Type Conversion

2:55 AMSunday, June 15, 2014
In Type casting you can change types or convert one types of data into another type.In c#, Basically there are two types. First one implicit type casting and another one is explicit type casting. Both are very useful in c# application development like, in web application text box return by default text value and you want to take int type value then you must convert text to int.
Implicit type conversion - conversion is performed by c# compiler automatically with type-safe manner. Now lets to take an simple example here.

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Double d;
            int a=15;
            d = a;
            Console.Write(d.ToString());
            Console.ReadKey();
        }
    }
}
Code Generate the following output
Implicit type conversion


Explicit type conversion - conversion is performed by programmer explicitly using the pre-defined functions. Suppose you want to change double type value into int type then must use pre-defined functions. This type of conversion is not safe. Lets take an simple example.

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            Double d = 3.14;
            int a;
            a = (int)d;
            Console.Write(a.ToString());
            Console.ReadKey();
        }
    }
}
Code generate the following output
Explicit type conversion
So there are various types available in c#, these are
ToBoolean : Convert one type to boolean type , if possible
ToByte : Convert one type to Byte type , if possible
etc.
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top