Type Casting in C#

Learn via video courses
Topics Covered

Overview

In a programming language, Type casting, also known as type conversion, is the process of converting one data type into another data type. This conversion is required when performing operations or assignments on variables of various data types.

Type casting is useful when we need to perform operations or use variables of different data types in our code to meet specific business requirements.

What is Type Casting in C#?

Type Casting allows us to change the data type of a value to another data type based on specific requirements. However, it can only be done if both data types are compatible, otherwise, the compiler will show an error. In C#, which is statically typed, once a variable is declared, it cannot be redeclared with another type during compilation. This means that we can't directly assign a value of one type to a variable of another type unless there's an implicit conversion.

There are two types of type casting in C# implicit type casting and explicit type casting. Implicit type casting is an automated conversion performed by the compiler for safe data conversions. Explicit type casting, on the other hand, requires manual intervention and should be used with caution to avoid data loss.

Consider an example of converting a string to an int data type in C#. In C#, we cannot directly convert a string to an int without explicit type casting. So, If we declare a variable named val as an int and try to assign the string value 546 to it, this will result in an error because there's no automatic or implicit conversion between these two data types.

Output

To make the above conversion possible, it is necessary to perform explicit type casting using suitable methods or functions.

Implicit Type Casting

Implicit type casting in C#, also known as implicit type conversion, is an automated process in programming languages that converts a value of one data type to another data type automatically and safely without the use of explicit casting operators or functions. This conversion occurs when two data types are compatible, and the target data type can hold a wider range of values than the source data type.

For example, in C#, you can implicitly convert an int to a long or a float to a double since the target types can accommodate the values of the source types with no data loss. However, you cannot convert a string to an int implicitly since it may result in data loss.

The table below shows the implicit types of conversion that C# supports:

Convert from Data TypeConvert to Data Type
byteshort, int, long, float, double
shortint, long, float, double
intlong, float, double
longfloat, double
floatdouble

Example:

Output:

In the above example, we have declared an integer variable called val1 with the value 50. The key observation is the line double val2 = val1;, where we assign the int variable's value to a double variable. The compiler automatically converts the int value to a double because both types are numeric and compatible. Since int occupies 4 bytes of memory and double occupies 8 bytes, there is no issue storing the 4-byte data inside the 8-byte memory location. The example uses the GetType() method to check the data types of val1 and val2 variables.

Explicit Type Casting

Explicit type casting in C#, also known as explicit type conversion, is the manual process of converting a value from one data type to another in a programming language. In contrast to implicit type casting, explicit type casting requires the use of casting operators or functions to perform the conversion. This is a risky type of conversion since there is a chance of data loss or the conversion failing for whatever reason.
In Explicit Type Conversion, we explicitly convert one data type to another data type, where larger data types like double or long (with huge memory sizes) are converted to smaller data types like int, byte, short, float, and so on (with small memory sizes).

Example:

Output:

The above code gives an error because when assigning a value of a larger data type to a smaller data type, explicit type casting is required.

Here is the corrected code:

Output:

In the above example, we have declared a double variable called val1 with a value of 50.45. The key observation is the line int val2 = (int) val1;, where we assign the double variable's value to an int variable. After the type conversion, the initial value of 50.45 has been converted to 50. That indicates we lost some information during the type conversion. This is because we are explicitly converting the larger data type double to the smaller data type int.

Type Conversion Methods

There are built-in type conversion methods in C# that provide a convenient way to convert between different data types.

Here's a list of various frequently used built-in type conversion methods:

MethodDescription
ToBooleanIt converts a value to a Boolean (true or false) value.
ToCharIt converts a value to a character data type.
ToByteIt converts a value to a byte data type.
ToDecimalIt converts a value to a decimal data type.
ToDoubleIt converts a value to a double data type.
ToInt16It converts a value to a 16-bit integer data type.
ToInt32It converts a value to a 32-bit integer data type.
ToInt64It converts a value to a 64-bit integer data type.
ToStringIt converts a given value to its string representation.
ToUInt16It converts a value to an unsigned 16-bit integer data type.
ToUInt32It converts a value to an unsigned 32-bit integer data type.
ToUInt64It converts a value to an unsigned 64-bit integer data type.

Conclusion

  • Type casting in C#, also known as type conversion, is the process of converting one data type into another data type.
  • In C#, once a variable is declared with a specific data type, we cannot declare it again with another type during the compilation process due to the language’s statically-typed nature.
  • There are two types of type casting in C# implicit type casting and explicit type casting.
  • Implicit type casting in C# is an automated conversion performed by the compiler for safe data conversions.
  • Explicit type casting, on the other hand, requires manual intervention and should be used with caution to avoid data loss.
  • In contrast to implicit type casting, explicit type casting requires the use of casting operators or functions to perform the conversion.