When writing custom classes we can provide behaviour to allow for both explicit and implicit conversions to other types.
Implicit conversion operators are those that don’t require an explicit cast.
Explicit conversion operators are those that do require an explicit cast.
As an example, the following code shows a simple console application to covert a weight in Pounds to Kilograms.
internal class Program
{
private static void Main(string[] args)
{
WriteLine("Please enter a value in lbs");
var lbsText = ReadLine();
var lbs = new PoundsExplicit(float.Parse(lbsText));
WriteLine($"\nYou entered {lbs}");
Kilogram kg = (Kilogram) lbs;
WriteLine($"\n{lbs} is {kg}");
WriteLine("\nPress enter to exit");
ReadLine();
}
}
Notice in the preceding code the line Kilogram kg = (Kilogram) lbs; is explicitly casting from a type of PoundsExplicit to Kilograms.
The Kilogram class is defined as follows:
internal class Kilogram
{
public float Weight { get; set; }
public Kilogram(float weight)
{
Weight = weight;
}
public override string ToString()
{
return $"{Weight} kg";
}
}
To allow the (explicit) cast from PoundsExplicit to Kilogram, the PoundsExplicit class defines an explicit conversion operator as shown in the following code:
internal class PoundsExplicit
{
public float Weight { get; set; }
public PoundsExplicit(float weight)
{
Weight = weight;
}
public override string ToString()
{
return $"{Weight} lbs";
}
public static explicit operator Kilogram(PoundsExplicit lbs)
{
const float conversionRate = 0.45359237f;
float equivalentKgs = lbs.Weight * conversionRate;
return new Kilogram(equivalentKgs);
}
}
To allow the conversion to Kilogram to be implicit, with no cast required (e.g. Kilogram kg = lbs;) the operator can be changed to implicit as shown in the following class:
internal class PoundsImplicit
{
public float Weight { get; set; }
public PoundsImplicit(float weight)
{
Weight = weight;
}
public override string ToString()
{
return $"{Weight} lbs";
}
public static implicit operator Kilogram(PoundsImplicit lbs)
{
const float conversionRate = 0.45359237f;
float equivalentKgs = lbs.Weight * conversionRate;
return new Kilogram(equivalentKgs);
}
}
The main method can now be modified as follows:
private static void Main(string[] args)
{
WriteLine("Please enter a value in lbs");
var lbsText = ReadLine();
var lbs = new PoundsImplicit(float.Parse(lbsText));
WriteLine($"\nYou entered {lbs}");
Kilogram kg = lbs;
WriteLine($"\n{lbs} is {kg}");
WriteLine("\nPress enter to exit");
ReadLine();
}
Notice in the preceding code the conversion from PoundsImplicit to Kilogram now is implicit, it does not require an explicit cast.
When deciding between implicit or explicit conversion operators you should consider the resulting readability of code that uses the types. An implicit cast is more convenient whereas an explicit cast may add extra clarity/readability to the code that uses the types.
If you want to fill in the gaps in your C# knowledge be sure to check out my C# Tips and Traps training course from Pluralsight – get started with a free trial.
SHARE: