Sunday, May 17, 2020

C# Tutorial: 06 - Type Casting

Tutorial 06: Type Casting

Changing variable of one type to another is referred as type casting. For example, if you assign floating point value to integer variable. Type casting can be of two types:
  1. Implicit Casting
  2. Explicit Casting
1. Implicit Casting
    Type casting which is performed by compiler automatically is referred as implicit type casting      e.g.
    char > int > long > float > double
    For example:
   
2. Explicit Casting
    Type casting which is performed by programmer manually is called explicit type casting e.g.
    double > float > long > int > char
    For example:


Type Casting Methods in C#
There are casting methods available in C# in order to to convert various data types.
Convert.ToBoolean() // To convert into boolean value
Convert.ToChar()      // To convert into char
Convert.ToInt()         // To convert to int
Convert.ToInt16()     // To convert into short
Convert.ToInt32()     //  To convert into int
Convert.ToInt64()    // To convert into long
Convert.ToFloat()    // To convert into float
Convert.ToDouble()  // To convert into double
Convert.ToString()  // To convert into string
For example see following:

No comments:

Post a Comment