Type Casting In C# - Implicit Vs Explicit

Introduction

Type casting in C# refers to converting one data type to another. It is a fundamental concept in programming, especially when working with different data types. C# supports two types of type casting: implicit and explicit.

Implicit Casting in C#

Implicit casting happens automatically when the conversion is safe and does not require extra code. Implicit casting works when the target type is larger than the source type. For example, an int can be implicitly cast to a long, float, or double.

int num1 = 10;
long num2 = num1;

In this example, the integer variable num1 is implicitly cast to a long when it is assigned to the num2 variable.

Explicit Casting in C#

Explicit casting works when the target type is smaller than the source type or when converting between incompatible types. Explicit casting requires the programmer to define the conversion using the appropriate cast operator explicitly. The syntax for explicit casting encloses the expression to be cast within parentheses, followed by the target type in square brackets.

double num1 = 10.5;
int num2 = (int)num1;

Let’s look at a simple story to understand casting in C# better. In this example, we are casting the double value num1 to an integer using the (int) cast operator. The resulting value is 10, the integer part of the double number.

 

Once upon a time, a group of animals was in a forest. They all had different attributes, such as height, weight, and age. One day, the animals decided to organize a race. The winner would be the animal that could run the fastest.

The race started, and all the animals began running. The first animal to cross the finish line was a cheetah. The cheetah was speedy, but the problem was that the race organizers had asked for the speed in kilometers per hour, and the cheetah’s speed was in meters per second.

The organizers asked the cheetah to convert its speed into kilometers per hour. The cheetah did not know how to do it, so it asked a wise old owl for help. The owl told the cheetah to use casting to convert its speed from meters per second to kilometers per hour.

The cheetah listened carefully to the owl’s instructions and wrote the following code:

double speedInMetersPerSecond = 25.0;
double speedInKilometersPerHour = speedInMetersPerSecond * 3.6;
int speedRounded = (int)speedInKilometersPerHour;

The cheetah used the formula speedInMetersPerSecond * 3.6 to convert its speed from meters per second to kilometers per hour. The resulting value was 90.0. Since the organizers had asked for an integer value, the cheetah used the (int) cast operator to convert the value to an integer. The resulting value was 90.

The cheetah gave the organizers value 90 and won the race. From that day on, the cheetah knew how to use casting to convert its speed into different units, and it became even faster.

Conclusion

In conclusion, type casting in C# is an essential programming concept that allows converting one data type to another. It can be either implicit or explicit, depending on the circumstances. By understanding the types of typecasting and their respective methods, programmers can write more efficient and effective code.


Similar Articles