C# Tutorial C# Advanced C# References

C# - Type Casting



The type casting is a method of converting the value of one data type to another data type. It is also known as type conversion. In C#, there are several kinds of conversions. However, in this tutorial we will focus only on two major types.

  • Implicit Type Casting
  • Explicit Type Casting

Implicit Type Casting

The implicit type casting happens automatically when converting a smaller data types to larger data types. The compiler implicitly typecast the smaller data types to the larger data types. No data will be lost in this process.

C# Implicit Casting

Example

The example below shows how the implicit type casting is done in C#.

using System;
 
class MyProgram {
  static void Main(string[] args) {   
    byte num_byte = 10;

    //implicit Casting
    short num_short = num_byte;
    int num_int = num_short;
    long num_long = num_int;
    float num_float = num_long;
    double num_double = num_float;

    //printing variables
    Console.WriteLine("num_byte = {0}", num_byte); 
    Console.WriteLine("num_short = {0}", num_short);
    Console.WriteLine("num_int = {0}", num_int);
    Console.WriteLine("num_long = {0}", num_long);
    Console.WriteLine("num_float = {0}", num_float); 
    Console.WriteLine("num_double = {0}", num_double); 
  }
}

The output of the above code will be:

num_byte = 10
num_short = 10
num_int = 10
num_long = 10
num_float = 10
num_double = 10

Explicit Type Casting

The explicit type casting does not happen automatically. It is performed manually by calling the compiler explicitly to typecast the larger data types into the smaller data types. There might be a data loss in this process.

C# Explicit Casting

Example

The example below shows how the perform explicit type casting by calling the compiler explicitly.

using System;
 
class MyProgram {
  static void Main(string[] args) {    
    double num_double = 10.5;

    //explicit Casting
    float num_float = (float) num_double;
    long num_long = (long) num_float;
    int num_int = (int) num_long;
    short num_short = (short) num_int;
    byte num_byte = (byte) num_short;

    //printing variables
    Console.WriteLine("num_double = {0}", num_double);
    Console.WriteLine("num_float = {0}", num_float); 
    Console.WriteLine("num_long = {0}", num_long);
    Console.WriteLine("num_int = {0}", num_int);  
    Console.WriteLine("num_short = {0}", num_short);
    Console.WriteLine("num_byte = {0}", num_byte);
  }
}

The output of the above code will be:

num_double = 10.5
num_float = 10.5
num_long = 10
num_int = 10
num_short = 10
num_byte = 10

Type Conversion methods

In C#, there are several built-in methods which can be used for type conversion. These methods are mentioned below:

MethodsDescription
ToBooleanConverts a type to a Boolean value, if possible.
ToByteConverts a type to a byte type, if possible.
ToCharConverts a type to a single Unicode character, if possible.
ToDateTimeConverts a type (integer or string) to a date-time value.
ToDecimalConverts a floating point or integer type to a decimal number.
ToDoubleConverts a type to a double-precision floating-point number.
ToInt16Converts a type to a 16-bit signed integer.
ToInt32Converts a type to a 32-bit signed integer.
ToInt64Converts a type to a 64-bit signed integer.
ToSbyteConverts a type to a signed byte type, if possible.
ToSingleConverts a type to a single-precision floating-point number.
ToStringConverts a type to a string.
ToTypeConverts a type to a specified type.
ToUInt16Converts a type to an unsigned 16-bit unsigned integer.
ToUInt32Converts a type to an unsigned 32-bit unsigned integer.
ToUInt64Converts a type to an unsigned 64-bit unsigned integer.

Example

The example below shows how the perform narrowing conversion by calling the compiler explicitly.

using System;
 
class MyProgram {
  static void Main(string[] args) {    
    bool x = true;
    float y = 50.55f;
    
    //using type conversion methods
    Console.WriteLine(x.ToString());
    Console.WriteLine(y.ToString());

    //another way of using methods
    Console.WriteLine();
    Console.WriteLine(Convert.ToString(x));
    Console.WriteLine(Convert.ToString(y));
  }
}

The output of the above code will be:

True
50.55

True
50.55