C# Implicit vs Explicit Conversion: Differences + Examples

C# Implicit vs Explicit Conversion: Differences + Examples
December 8, 2021
4 minutes read

After many years programming in .NET, you may have already realized that the framework allows us, in a very simple way, to make conversions between different types of data.

Conversions can be of 2 types 👇

Implicit Conversions

Implicit conversions are for which it is not necessary to indicate the conversion in parentheses (…).

Loading code snippet...

In the code we assign a variable of type double a value of type int.

But the compiler doesn’t tell us anything and lets us continue working as if nothing had happened. This is known as an implicit conversion.

Instead, if we do the following 👇

Loading code snippet...

The compiler will give us an error indicating that an implicit conversion from double to int cannot be done, and tells us to use an explicit conversion.

Explicit Conversions

There is no mystery about it, simply this problem can be fixed by putting (int) in front of 10.0 👇

Loading code snippet...

Placing the type of data to which we want to convert it in parentheses is called explicit conversion. What this means is that with this syntax we are explicitly indicating to the compiler that we want to convert one type of data into a different one.

At this point you could perfectly tell me that this would also work 👇

Loading code snippet...

Apparently it does the same as if we don’t put (double) in front of it.

So… where is the difference? 🤔

The only difference between implicit and explicit conversions has to do with whether or not there is a risk of that information being lost.

If we go to the first case (without parentheses), it is an implicit conversion. The value of the litetal int does not matter as if we write double since there will be no risk of losing the information as it is a type of greater capacity.

On the contrary, if we go to the second case, we are forced to do an explicit conversion. This works because a double object can contain values ​​that a smaller capacity int type cannot represent.

Let’s take an example 👇

Loading code snippet...

How we can create our own conversions

Usually the conversions of the Framework are more than enough for the needs that arise. However, there may be many cases where it would be more useful to define our own implicit and explicit conversions. The .NET platform provides us with an easy way to do it.

To create a conversion between two types, we simply have to write the corresponding operator, either implicit or explicit.

We are going to create an imaginary scenario in which we have to manage the temperatures so that the degrees can change between Celsius and Fahrenheit.

Let’s create the code 👇

Loading code snippet...

Now with this code we have the possibility of writing methods so that we do the conversion between them, and which we call each time.

Loading code snippet...

This works, but we will have to call the corresponding method each time and this makes the code very messy.

If we want to do an implicit conversion, we simply have to define a static operator with the static implicit operator.

We go back to our example 👇

Loading code snippet...

We can see that to each class we add an implicit conversion operator for the other related class, and after that, we just implicitly perform the conversions.

What we gain from this is a much cleaner code that meets the conditions for implicit conversions.

If we want to do it differenty

  • Let’s imagine that we have an application that has to manage the teachers and students of a school.

Let’s see it better 👇

Loading code snippet...

If at some point a student becomes a teacher or vice versa, we will need to use a conversion to reuse the data. As in this case, we are going to lose information since the different classes do not handle the same information.

Therefore the conversion will have to be explicit 👇

Loading code snippet...

Advanced Conversion Techniques

Next, we’re going to dive into some advanced conversion techniques that you can’t miss. And yes, I promise I won’t bore you this time!

Using System.Convert for Conversions

Don’t forget that C# provides the System.Convert class which comes preloaded with a bunch of handy conversion methods. This helps when you need to handle conversions in a safer and more controlled manner. Take a look at this example:

Loading code snippet...

You can also handle conversion scenarios that might throw exceptions, by using methods like Int32.TryParse(), giving you the opportunity to take alternative action.

Loading code snippet...

It always pays off to be cautious, doesn’t it?

Dealing with Nullable Types

Nullable types sure can be tricky! But with explicit and implicit conversions combined with the power of C#, you can easily handle nullable types like a pro. Check this out:

Loading code snippet...

Just remember to be extra careful with nulls, as they can lead to runtime exceptions if not handled properly. You’ve been warned! 🙂

Real-Life Examples

Currency Conversion Example

Here’s an example of currency conversion done the C# way, using custom implicit and explicit conversions:

Loading code snippet...

It’s like magic, isn’t it?

Handling Complex Data Structures

When working with complex data structures, you may need to apply multiple explicit and implicit conversions in sequence to achieve your desired result. Let’s see a real-life example with a graph:

Loading code snippet...

The Real Deal: Common Pitfalls & Best Practices

  • Remember that implicit conversions should be used only when there’s no risk of data loss, then you can use explicit conversions.
  • Keep the use of user-defined conversions to a minimum, and avoid overloading
  • operators unnecessarily, as it can lead to confusion and decreased readability.
  • Whenever possible, use built-in conversion methods such as System.Convert and TryParse methods for a safer and more controlled way to handle conversions.
  • Be cautious when dealing with nullable types and null reference exceptions. Always validate your input data and handle nullability appropriately.

Going the Extra Mile: Making Your Code Efficient

When working with conversions, always strive to ensure your code is efficient on both memory and performance fronts. Here are some tips to help you achieve that:

  • Cache and reuse conversion factors and other frequently used data, rather than recomputing them.
  • If you’re implementing custom conversion operators, make sure that they exhibit consistent behavior across different types.
  • When dealing with large datasets or high performance environments, consider using specialized libraries for performing conversions. They often provide better optimization and performance than hand-written custom conversions.

Keep Your Code Clean & Organized

Above all, keep your code clean and organized by using appropriate comments and method names. Proper organization and documentation of your code will greatly improve its readability and maintainability in the long run.

Parting Thoughts

Mastering the art of implicit and explicit conversions in C# is crucial to writing effective and efficient code, particularly in complex scenarios. Remember to follow best practices and keep an eye out for pitfalls, and your code will be more robust, maintainable, and well-organized.

Conclution

Marking a conversion as implicit or explicit must strictly meet the criteria of whether there is a risk of information loss. It may be the case that today you consider the loss of information that occurs to be negligible, but that at some point it is not.

If you have marked the conversion as implicit, anyone who uses it directly assumes that there is no information loss.

If there is later and the failure is in that conversion, it can be a big headache, even more so if the code is part of a library and the person who uses it cannot see the code. From ByteHide we recommed you always think about it before defining the conversion. 🤗

You May Also Like

Optional Parameters in C#: What You Need to Know

Optional Parameters in C#: What You Need to Know

Programming in C# often requires flexibility and simplicity ...

What is Lock Keyword in C#? Main Usages

What is Lock Keyword in C#? Main Usages

IndexUnderstanding the C# Lock Keyword Can’t tell a lock fro...

Enumerate in C#: Detailed Explanation

Enumerate in C#: Detailed Explanation

Desperate to decode the mystery that is enumeration in C#? I...

Creating a JSON Class in C#: Detailed Guide

Creating a JSON Class in C#: Detailed Guide

In the world of application development, manipulation of dat...

Static Class in C#: How to Use It?

Static Class in C#: How to Use It?

Hello there, future C# aficionado! It’s time to roll down th...

DateTime Formatting in C#: Dev Guide

DateTime Formatting in C#: Dev Guide

Have you ever gotten frustrated handling dates and times in ...

Leave a reply

Loading comment form...