C# type casting

Casting is about storing a value on type into a variable of different type.

For example let’s say we have an integer value 10. Obviously this value can be stored in a variable of type int. But in C# we’re allowed to store this value into a variable of type double or float as well!

For this particular case all we need to do is to assign this value into a variable of type double.

Example:

int i = 10;
double d = i;

Behind the scene the compiler will allocate a memory space for the variable d and stores the value 10 as the type double in this variable.

Widening casting happens automatically by the computer and there’s no work on developers.

The case where widening casting happens is:

When we want to store the value of a variable with lower memory space to a variable of higher memory space. Remember that the type of the value should be in same family (for example it should be a number etc.)

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

As the list above shows a byte-type value takes less memory space compared to short-type so a byte value can be stored in a short variable. Or a value of type int can be stored in a variable of type long again because it takes less memory space etc. and no-compile time error will occur.

On the other hand we have Narrowing Casting (AKA explicit casting):

This type of casting happens when a value of a data type with higher memory space is about to be stored in a variable with lower memory space.

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

In such cases, there’s a chance that part of the data will be lost.

By default the compiler won’t allow to narrow casting unless we explicitly tell the compiler to do so.

This is the way we run a narrow casting:

double d = 2.33;
int i = (int) d;

In this example, we want to store the value of the d variable which is of type double into the i variable which is of type integer.

You see, an integer variable can’t store a value that has fraction. Basically the compiler will return error if we attempt to store a value with fraction point to a variable of type integer.

The reason that compilers return error is that, the fraction part of the value will be lost if it’s going to be stored in a variable of type integer (Hence the value will be narrowed).

But with the help of narrowing casting we can signal the compiler that we are aware of the danger of data lost but we still insisting on doing so.

As the example above shows, in order to run a narrow casting, we place the target data type in parentheses in front of the value that we want to cast it.

Example:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
double height = 5.6;
int i = (int)height;
Console.WriteLine(i);
}
}
}

Output: 5

In this example, if we didn’t use the narrowing casting, the compiler would return error reminding us of the danger of losing data.

Let’s see this in action:

using System;

namespace quickExample

{
public class Program
{
public static void Main(string[] args)
{
double height = 5.6;
int i = height;
Console.WriteLine(i);
}
}
}

Output:

Cannot implicitly convert type ‘double’ to ‘int’. An explicit conversion exists (are you missing a cast?)