Implicit And Explicit Conversions In C#

In my last article about type casting, I explained narrow and widening conversions along with as and is operators. In this article, we shall take casting to a whole new level. At the end, you will learn how readability of code can be improved with implicit and explicit operators.

Implicit conversions

Implicit conversions are easier to use and understand. For example, assigning an integer to a double is implicit conversion and of course there is no data loss.

  1. int val1 = 10;  
  2. double val2 =val1;  
Conversion shown above is a valid conversion and it would be compiled successfully. However, other way round is not possible without casting the variable.
  1. int val1 = 10;  
  2. double val2 =val1;  
  3. int val3 = (int) val2;  
Therefore, converting from double to int is not allowed without type casting. It is called explicit type casting.

Consider the code below and casting done
  1. double amount=10;  
  2. Money money= new Money(amount);  
  3.   
  4. Int convertedAmount=Convert.ToInt16(money.Amount);  
  5.   
  6. //Money class  
  7. class Money  
  8. {  
  9.    public double Amount {get;set;}  
  10.   
  11.    public Money(double amount)  
  12.    {  
  13.       Amount=amount;  
  14.    }  
  15. }  
We pass a double to Money class constructor but we expect to show user amount from Money class in int. Therefore, we make use Helper classes from C# world, and convert the double amount in to integer.

By eliminating unnecessary casts, implicit conversions can improve source code readability. Nevertheless; we can use implicit and explicit operators at the class side to convert a value from one type to another. Let’s see a few examples to understand the concept better.

Implicit

Amount is a property of type double and we create a new object of class Money, we pass in to constructor the amount. When we want to use the value of the amount, we access it over the money object created (money.Amount). C# provides implicit operator which facilitates this conversion at the class end.
  1. public static implicit operator double(Money m)  
  2. {  
  3.    return m.Amount;  
  4. }  
  5.   
  6.   
  7. Usage:  
  8. Money m = new Money(12.3);   
  9. double amount = m;  
The above statement calls the static function double (the return value) with m (Type Money) and returns Amount set through constructor in double format.

With the help of implicit operator, readability of code is improved and now class Money is responsible for all the conversion needed.

Explicit

Let’s understand it the other way round. Amount cannot be converted in to Money; therefore, we need explicit casting.
  1. public static explicit operator Money(double m)  
  2. {  
  3.    Money money = new Money(m);  
  4.    return money;  
  5. }  
Usage
  1. double amt = 16.5;  
  2. //Explicit Conversion  
  3. Money money = (Money) amt;   
  4. Console.WriteLine(money.Amount);  
So as to summarize, the implicit keyword should be used to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.

However, to prevent unexpected results, care must be taken care dealing with implicit conversions because it does not require programmers to explicitly cast from one type to the other. However, explicit conversion is quite obvious with casting.

 

Up Next
    Ebook Download
    View all
    Learn
    View all