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.
- int val1 = 10;
- 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.
- int val1 = 10;
- double val2 =val1;
- 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 - double amount=10;
- Money money= new Money(amount);
-
- Int convertedAmount=Convert.ToInt16(money.Amount);
-
-
- class Money
- {
- public double Amount {get;set;}
-
- public Money(double amount)
- {
- Amount=amount;
- }
- }
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.
- public static implicit operator double(Money m)
- {
- return m.Amount;
- }
-
-
- Usage:
- Money m = new Money(12.3);
- 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.
- public static explicit operator Money(double m)
- {
- Money money = new Money(m);
- return money;
- }
Usage - double amt = 16.5;
- //Explicit Conversion
- Money money = (Money) amt;
- 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.