• Now Online : 45
  • admin@codemyne.net


Type Casting (or) Conversion: Type casting (or) conversion is to convert data of one type to data of another type. It is often required in C#; type conversion is of two types as shown in figure:

Implicit Conversion: If a compiler converts one type of data into another type of data automatically, it is known as implicit conversions. There is no data loss in implicit conversion.

In implicit casting, conversion always takes place from lower to higher. Implicit is always under control of CLR. Here is an example of implicit conversion:

    short a = 20;
    //Implicit conversion
    int b = a;

Explicit Conversion: When the data of one type is converted explicitly to another type with the help of some pre-defined functions it is called as explicit conversion and there may be data loss in this process because the conversion is forceful. These are some of the conversions that cannot be made implicit:

  • int to short
  • int to uint
  • uint to int
  • float to int

C#.Net supports following types of Explicit Type Casting.

  1. C++ Style Type Casting
  2. Converting
  3. Parsing

C++ Style Type Casting: Here is the syntax for C++ style type casting.

    datatype1 v1;
    datatype2 v2= (datatype2) v1;

Note: The type in parentheses is the target data type. There may be loss of data in casting process if you are casting to a smaller type. Here is an example showing casting:

int i = 257;
byte sal = (byte)i;
// gets max value of byte
                                        //here possible for loosing the data 

Converting: Convert is a pre-defined class. Working with convert class is called as converting.

Methods of Convert class:


Note: There is also method to convert between numeric values and strings in C#;

int x = 20;
// x has been converted to string and its value will be as string '20' now
string str = x.ToString();

Parsing: As per C#.Net all the datatypes are pre-defined structures. Structure is a collection of methods. Every datatype contains a set of methods as maxvalue, minvalue, parse(), Tostring()

string str = "20";
// Now the value of y is int '20'
int y = int.parse(str);          

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment

kiran 2/26/2011 (IST) / Reply

this is really gud

nandini 10/20/2011 (IST) / Reply

this gud website ..please provide conversions coding it will makes better understanding to us .this is my humble request.

cembo 1/31/2012 (IST) / Reply

... helpful ...

cembo 1/31/2012 (IST) / Reply

... thanx man, would be very good to see some topic about class casting ...

Imran khan 3/14/2012 (IST) / Reply

this topic is very help full thanks :)

PluCiorx 4/10/2012 (IST) / Reply

Missing description of As keyword used for as cast operator.

SurendarReddy.G 7/6/2012 (IST) / Reply

It is a good website for fresher students.Iam encourage you to provide some more atricles on basic concepts.

sandeep 5/11/2013 (IST) / Reply

hi......................... Renuka,i like your article.so much thanks u.

Thyagaraj 7/23/2013 (IST) / Reply

It is website, who are want be developers............. I am happy to saw this website ..moreover I am expecting more articles from C#.net

sudeep 7/3/2014 (IST) / Reply

what a fantastic explaination.i have pleased and very thanks full

Kamlesh Joshi 7/5/2014 (IST) / Reply

Whats Disadvantages & Limitations of Polymorphism in C# ?

ravi 6/2/2016 (IST) / Reply

i want to insert a value from textbox using marathi language and database column is of float datatype .how to do conversion of varchar to float?