Type Casting

Learn how to cast an object to another type.

We'll cover the following...

Introduction

In this lesson, we’ll explore the concept called type casting, where we convert an object of one type to an object of another type.

Consider the following example:

C#
using System;
namespace TypeCasting
{
class Program
{
static void Main(string[] args)
{
byte age = 24;
int in10Years = age + 10;
Console.WriteLine(in10Years);
}
}
}

It’s safe to assume that the value of in10Years is 34. But, if we change the type of in10Years to byte, our code fails to compile:

C#
using System;
namespace TypeCasting
{
class Program
{
static void Main(string[] args)
{
byte age = 24;
byte in10Years = age + 10;
Console.WriteLine(in10Years);
}
}
}

It fails because arithmetic operations on whole numbers return a value of the int type. Because in10Years is now a byte, we can’t store an integer in there. To solve this problem, we cast the result of the arithmetic expression to the desired data type:

C#
using System;
namespace TypeCasting
{
class Program
{
static void Main(string[] args)
{
byte age = 24;
// We cast the result of the expression to byte
byte in10Years = (byte)(age + 10);
Console.WriteLine(in10Years);
}
}
}

Type casting is performed by putting the target data type inside parentheses and placing the whole thing before an expression or variable that we want to cast:

byte in10Years = (byte)(age + 10);

Shrink

In the example above, we shrink an int value into a byte. Although we won’t have any issues in the given code, shrinking can result in unexpected behavior. This is because we’re placing a value into an area of smaller size than the space the value occupies in memory:

byte age = 24;
byte someNumber = (byte)(age + 250);

The someNumber variable is a byte variable, and bytes can hold a whole number up to 255. Because 24 + 250 is 274, it’s too large for a byte to hold. Our typecast shrinks this value, producing an unexpected result:

C#
using System;
namespace TypeCasting
{
class Program
{
static void Main(string[] args)
{
byte age = 24;
byte someNumber = (byte)(age + 250);
Console.WriteLine(someNumber);
}
}
}

Our operation leads to an overflow because 274 is too large for a byte to handle.

Shrinking and careless arithmetic operations can lead to these overflows.

The following illustration helps visualize what shrinking looks like:

Expand

Casting to a data type that’s larger in size, however, is always safe:

Consider the following example:

byte age = 24;
int ageAsInt = (int)age;

This operation is safe because int has enough space to contain all possible values of the byte type. Such typecasts can happen implicitly without the programmer’s participation:

byte age = 24;
int ageAsInt = age; // Implicit conversion occurred

The compiler informs us if implicit conversion is impossible. In this case, we need to use explicit conversion by putting the target data type inside parentheses like (int)someVariable.

Understand that explicit type casting is possible for compatible data types. We can’t cast a string object to int, for instance.