Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I define a (explicit or implicit) conversion operator in C#? [closed]

A somewhat little-known feature of C# is the possibility to create implicit or explicit user-defined type conversions. I have been writing C# code for 6 years now, and I have never used it. So, I'm afraid I might be missing good opportunities.

What are legitimate, good uses of user-defined conversions? Do you have examples where they are better than just defining a custom method?

--

Turns out, Microsoft has some design guidelines about conversions, the most relevant of which is:

Do not provide a conversion operator if such conversion is not clearly expected by the end users.

But when is a conversion "expected"? Outside of toy number classes, I can't figure out any real-world use case.


Here's a summary of the examples provided in the answers:

  • Radians/Degrees/double
  • Polar/Point2D
  • Kelvin/Farenheit/Celsius

The pattern seems to be: implicit conversions are mostly (only?) useful when defining numerical/value types, the conversion being defined by a formula. In retrospect this is kind of obvious. Still, I wonder if non-numerical classes could also benefit from implicit conversions..?

like image 621
Eldritch Conundrum Avatar asked Aug 26 '12 01:08

Eldritch Conundrum


People also ask

What is the difference between implicit type conversion and explicit type conversion?

Implicit conversion is the conversion in which a derived class is converted into a base class like int into a float type. Explicit conversion is the conversion that may cause data loss. Explicit conversion converts the base class into the derived class.

Why explicit conversion is known as user-defined type conversion?

Explicit type conversion, also called type casting, is a type conversion which is explicitly defined within a program (instead of being done automatically according to the rules of the language for implicit type conversion). It is defined by the user in the program.

Does C allow implicit conversion?

Implicit Type Conversion is also known as 'automatic type conversion'. It is done by the compiler on its own, without any external trigger from the user. It generally takes place when in an expression more than one data type is present.

Which operator can be used to do type conversions?

operator int(); ... }; The operator overloading defines a type conversion operator that can be used to produce an int type from a Counter object. This operator will be used whenever an implicit or explict conversion of a Counter object to an int is required. Notice that constructors also play a role in type conversion.


1 Answers

You can use a conversion operator when there is a natural and clear conversion to or from a different type.

Say for example that you have a data type for representing temperatures:

public enum TemperatureScale { Kelvin, Farenheit, Celsius }

public struct Temperature {

  private TemperatureScale _scale;
  private double _temp;

  public Temperature(double temp, TemperatureScale scale) {
    _scale = scale;
    _temp = temp;
  }

  public static implicit operator Temperature(double temp) {
    return new Temperature(temp, TemperatureScale.Kelvin);
  }

}

Using the implicit operator you can assign a double to a temperature variable, and it will automatically be used as Kelvin:

Temperature a = new Temperature(100, TemperatureScale.Celcius);
Temperature b = 373.15; // Kelvin is default
like image 151
Guffa Avatar answered Dec 19 '22 04:12

Guffa