TypeCasting (or) Type Conversion

Converting From one datatype to another datatype is called as Typecasting (or) Type Conversion.
C#.NET supports 3 types of type casting
  1. Implicit Typecasting
  2. Explicit Typecasting 
  3. Boxing and unboxing
Implicit Typecasting:
Converting from smaller datatype to bigger datatype is called implicit typecasting. Implicit typecasting is controlled by CLR, their is no need to follow any syntax.
Example:
           Void main()
            {
               short sno=20;
               int ino=sno;
               Console.WriteLine("interger number is: "+ ino);
            }


Explicit Typecasting:
Converting from bigger size of datatype to smaller size of datatype is called implicit type casting. Explicit typecasting has to be controlled by programmer and has to follow the syntax.
C#.NET supports Explicit typecasting in 3 ways
  1. General style of typecasting(C++ style)
  2. Parsing
  3. Converting
General style of typecasting:
Syntax: 
             <Todatatype> <To variable>=<Todatatype><fromvariable>;
Example:
             Void Main()
              {
                 int a=22;
                 byte b=(byte)a;
                 Console.WriteLine("b = " + b);
              }
Output:   
            b=22
Disadvantage:
      There is Chance of loosing data.

Parsing:
Every datatype is represented with a predefined class and Parse() is a predefined method, which is member of all datatype classes. Parse method accepts only string value, which converts from string to any another datatype.
Syntax:
         <Todatatype> <To variable>=<Todatatype>.Parse("Value");
    Example:                                       
        int i=int.Parse(Console.ReadLine());
Example program:
               Void Main()
               {
                  Console.WriteLine("Enter ur name");
                  string name=Console.ReadLine();
                  Console.WriteLine("Enter ur age");
                  int age=int.Parse(Console.ReadLine());
                  Console.WriteLine("Enter ur salary");
                  decimal salary=decimal.Parse(Console.ReadLine());
                  Console.WriteLine("Name: " + name+"Age : " + age + "salary : "+ salary);
                }
Disadvantages:
Parse method accepts only string .Using parse method we can convert only string to any other datatype.

Converting:
Converting is a predefined class which contains all the data types conversion methods.Using converting we can convert from any datatype to other datatype.
Syntax:
         <Todatatype> <Tovariable>=convert.<datatype conversion method ("value")>
Example:
          Void Main()
          {
            Console.WriteLine("Enter a number");
            int a=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter a number");
            int b=Convert.ToInt32(Console.ReadLine());
            int c= a+b;
            Convert.WriteLine(c);
          }
Boxing and Unboxing:
Boxing: Converting from value type to reference type is called as boxing.For example converting from int to object.
Syntax:
         <To datatype><To variable> = <To datatype><To variable >
Unboxing: Converting from reference type to value type is called as Unboxing. For example converting from object to int.
Syntax:
         <To datatype><To variable> = <To datatype><To variable >
Example:
          Void Main()
          {
             int i=25;
             object o=i;   //Boxing
             Console.WriteLine("I = " + i);
             int j=(int)o;   //Unboxing
             Console.WriteLine("J = " + j);
           }
Click here for more  : C#.NET Language Basics

No comments:

Post a Comment