Type Casting in C#4 min read

C# allows us to convert the variables of one data type to another. This conversion referred to as type conversion or type casting. There are various ways to typecast variables, casting can be between a larger size type to a smaller size type or vice-versa.

There are two types of casting in C#:

  • Implicit Casting (automatic casting): Conversion of smaller type to a larger.
    char -> int -> long -> float -> double
  • Explicit Casting (manual casting): Conversion of a larger type to a smaller size type.
    double -> float -> long -> int -> char

Implicit Casting

For this kind of conversion, there is no need for special syntax. Implicit includes the conversion of a smaller data type to a larger size data type and conversions from derived classes to base classes, so there is no loss of data here. It is also known as Automatic Type Conversion.

Although the two data types need to be compatible with each other that is the numeric data types are compatible with each other but the automatic conversion is not supported to the conversion of numeric type to char or boolean.

As the Implicit conversion is done by the compiler itself, compiler first checks the type compatibility before the conversion. The compatibility is checked in the following order:

byte -> short -> int -> long -> float -> double

The following shows the implicit types of conversion that is supported by C#:

Convert fromConvert to
byteshort, int, long, float, double
shortint, long, float, double
intlong, float, double
longfloat, double
floatdouble

Example of implicit type casting in C# programming.

Output:


Explicit Casting

Explicit conversion is a manual conversion that it is done by the user themselves. We specifically do the explicit conversion to prevent data loss or maybe when the conversion is not succeeded and it is done by using a cast operator ().

This conversion is useful for incompatible data types where above automatic conversion cannot be done. This happens when data of a larger type is converted to data of a smaller type.

long double - > double -> float -> long -> short -> char

Example of explicit type casting in C# programming.

Output:


C# Type Conversion Methods

There are some built-in conversion methods provided by the C#.

MethodsDescription
ToBooleanThis converts a type to a Boolean value,
ToByteIt will convert a type to a byte.
ToCharIt will convert a type to a single Unicode character.
ToDateTimeIt will convert a type (integer or string type) to date-time structures.
ToDecimalIt will convert a floating-point or integer type to a decimal type.
ToDoubleIt will convert a type to a double type.
ToInt16It will convert a type to a 16-bit integer.
ToInt32It will convert a type to a 32-bit integer.
ToInt64It will convert a type to a 64-bit integer.
ToSbyteIt will convert a type to a signed byte type.
ToSingleIt will convert a type to a small floating-point number.
ToStringIt will convert a type to a string.
ToTypeIt will convert a type to a specified type.
ToUInt16It will convert a type to an unsigned int type.
ToUInt3It will convert a type to an unsigned long type.
ToUInt64It will convert a type to an unsigned big integer.

C# program to demonstrate some the built-in type conversion methods

Output:


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More