Type Conversions in C#

Adem Catamak
3 min readMay 13, 2021

--

In this article, we will talk about how we can make type conversions (casting) with implicit and explicit keywords.

Photo by Suzanne D. Williams on Unsplash

C# language is one of the strongly typed programming languages. In other words, if we want to assign an object to an object that we produce from a different type, we will get a compile time error. Let’s take a look at how we can avoid this situation with implicit and explicit keywords.

Implicit Conversions

If we want to assign an object belonging to class X to an object belonging to class Y in C# language, our code will not be compiled. In spite of this information, how can we assign the following int type object to a long type object?

int integerValue = 42;
long longValue = integerValue;

The keyword that allows us to do this conversion is implicit. implicit conversions are transform types that will not generate errors. It indicates that the types are related to each other. In this way, an int object can be assigned to a long object. We do not receive errors during this type of conversion.

Explicit Conversions

Can we assign an object of type long to an object we define as int? The answer is both yes and no. If we look at the code block below, we can see that we cannot make a direct assignment as in the previous example.

long longValue = 42;
int intValue = (int) longValue;

The reason we are unable to assign directly is because there is no guarantee that this conversion will be error-free. The limits of the values ​​that long type can take are wider than the int object. For this reason, OverflowException may occur during conversion, or we may not see the expected value in the int value due to data loss. For this reason, we cannot apply implicit conversions. Since both values ​​are numbers, this conversion can be done, but to be aware of this risk, an automatic conversion is not done by the language as in implicit conversions. Instead, with (), the software developer is expected to make an explicit conversion, being aware of the situation.

In short, we call the type conversions we make unconsciously implicit, and we call the type conversions we do consciously as explicit conversion.

Usage Scenerio

There were many value objects such as UserId, ProjectId, UserPassword in a project that I tried to develop by using the DDD concept. Most of these were structures with values ​​such as Guid and string. While creating these value objects, I was first creating an object of Guid type as in the example below. Then I was generating an object of UserId type by giving the value to my constructor method.

To avoid this situation, we can use the implicit transformation operator as in the example below. In this way, we can assign the Guid object directly to the UserId object.

Bonus

In scenarios where we do unconscious type conversion, we also do a similar operation to method overload. For example, let’s assume that we have a method that takes an object of UserId type and manipulates it. We can also call this method with an object of Guid type. The reason for this is the C# language’s ability to activate the method by automatically converting the Guid object to the UserId type.

I hope it has been a useful post. You can click on this link to browse my other articles.

--

--

Adem Catamak
Adem Catamak

Written by Adem Catamak

var software = ConvertFrom(caffeine)

No responses yet