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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …