User-Defined Conversions
Earlier I mentioned that the parentheses used for casting cannot be overloaded and that user-defined conversionsmust be used instead. In short, user-defined conversions enable you to declare conversions on structures or classes such that the struct or class can be converted to other structures, classes, or basic C# types. Why and when would you want to do that? Let's say you needed to use the standard Celsius and Fahrenheit temperature scales in your application such that you could easily convert between the two. By creating user-defined conversions, you could use the following syntax: -
Fahrenheit f = 98.6F; Celsius c = (Celsius)f; // User-defined conversion.
This doesn't provide any functional benefits over the following syntax, but it is more intuitive to write and easier to read.
Fahrenheit f = 98.6F; Celsius c = f.ConvertToCelsius();