I want to convert from an enum to a string using explicit operator casting logic. Why am I getting a “User-defined conversion must convert to or from the enclosing type”? It should be possible to do that since the types are sub-types of the enclosing types.
class ConversionDemo{
public enum Letters { A, B, C };
public String LetterName { get; }
public void DoWork(ConversionDemo.Letters letter) {
this.LetterName = (String)letter;
}
public static explicit operator String(ConversionDemo.Letters letter) {
if (letter == Letters.A) { return "A"; }
if (letter == Letters.B) { return "B"; }
if (letter == Letters.C) { return "C"; }
return "?";
}
public static explicit operator ConversionDemo.Letters(String s) {
if (s == "A") { return Letters.A; }
if (s == "B") { return Letters.B; }
return Letters.C;
}
}