Type Casting Techniques in C#

Type casting is a concept of converting from one datatype into another datatype. C#.Net supports 2 types type casting techniques.

1)Implicit type casting : It is converting lower to higher.

 

2)Explicit type casting: It is converting higher to lower.

 

->we convert float to double is implicit type casting. 

->we convert long to int is explicit type casting. 

->we convert int to float is implicit type casting. 

->we convert float to int is explicit type casting.

 

C#.Net supports 4 types of explicit typecasting techniques: 

1)C++ style of type casting 

2)converting 

3)parsing 

4)boxing&unboxing

 

Working with C++ style of type casting: 

EX: int i=10; 

      Byte b=i;

 

Syntax: data type1 v1=value; 

              data type2 v2=(datatype 2)v1;

 

Example on c++style of typecasting: 

 Open console application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select console application

 

code for main method 

class program 

{ 

        static void Main(string[] args) 

        { 

            int x = 150; 

            Byte sal = (byte)x; 

            Console.WriteLine("salary is :" + sal); 

            Console.ReadKey(); 

        }

}

 

Execute the project(F5)

Note: in above program, If int x=256, then output will be 0, i.e in C++ style of type casting there is a possibility for losing the data.  

Syntaxes of Conditional statements:

Syntax of if condition:

 

1)If(condition)

Statement1; 

Else 

Statement2;

 

2)if(condition) 

{ 

       Statements

} 

Else 

{ 

      Statements

}

 

3)syntax of nested if condition

If(condition1) 

Statement1; 

Else if(condition2) 

Statement2;

Else 

Statement;

 

4)Syntax of switch condition 

 Int x=1; 

Switch(x)

{ 

Case 1: statements; 

Break;

Case 2: statements; 

Break; 

Default: statements; 

Break;

}

 

Syntax of loops:

 

1)for loop syntax 

For(initialization;condition;increment(or) decrement)

 

Syntax of while loop: 

While(condition) 

{ 

 Statements

}

 

Syntax of do-while loop:

Do 

{ 

 Statements; 

} 

While(condition);

 

Working with converting concepts:

converting is used to typecast from any datatype into any another datatype. Convert is a pre defined class. Working with convert class is called as converting, class contains a collection of methods

 

Methods of convert class: 

Byte b=convert.ToByte(value) 

SByte b=convert.TosByte(value) 

Double d=convert.ToDouble(value) 

Bool x=convert.ToBoolean(value) 

Char c=convert.ToChar(value) 

Float x=convert.Tosingle(value) 

Short x=convert.Toint16(value) 

Ushort x=convert.Touint16(value)

 

Example on converting:

 

Open console application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select console application

 

code for main method 

class program 

{ 

        static void Main(string[] args) 

        { 

            //print ASCII chars 

            for (int i = 1; i < +256; i++) 

            { 

                char c = Convert.ToChar(i); 

                Console.Write(c); 

            }//close for loop 

            Console.ReadKey(); 

        } 

}

 

Execute the project(F5)

 

Working with parsing:

 

Working with parse method is called as parsing. Parsing can be used only to convert from string data type into any another datatype. 

 

Example on maxvalue,minvalue and parse method concepts:

 

Open console application project:

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select console application

 

code for mainmethod:

 

class program 

{ 

        static void Main(string[] args) 

        { 

            Console.WriteLine(int.MaxValue); 

            Console.WriteLine(int.MinValue); 

            Console.WriteLine("enter ur sal:"); 

            String s = Console.ReadLine(); 

            //always reads the data in the format of string 

            String ts = s + 2000; 

            Console.WriteLine("before parsing:" + ts); 

            int sal = int.Parse(s); 

            sal = sal + 2000; 

            Console.WriteLine("after parsing:" + sal); 

            Console.Readkey(); 

        } 

}

 

Working with Boxing & UnBoxing:

1)Boxing is a concept of converting from value types into reference type and vicevarsa is called as unboxing.

2)Boxing is an implicit typecasting and unboxing is an explicit typecasting.

3)object is a predefine reference datatype.

4)object datatype is capable to hold any type of data

 

Example on Boxing & unBoxing:

 

Open console application project:

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->

project->select visual c# from installed templates->select console application

 

code for main method:

 

class program

{

        static void Main(string[] args)

        {

            int i = 10;//value type, int is a structure

            Object x = i;//object is a class boxing

            Console.WriteLine("after Boxing:" + x);

            Object k = 1234;

            int n = (int)k;  //unboxing

            Console.WriteLine("after unboxing:" + n);

            Console.Readkey();

        }

 

}