c# - Implicit operator for int? -
i've learned use implicit operators classes.
now want this:
public static implicit operator string(int val) { return "the value is: " + val.tostring(); // nonsense example }
however wrong. compiler says:
user-defined conversion must convert or enclosing type
how can workaround this?
my goal able run code this:
int x = 50; textbox1.text = x; // without using .tostring() or casting
you can't add operator outside of class of either parameter or return type of operator.
in other words: can't add operator converts between 2 types both don't control.
the compiler error explicit this:
user-defined conversion must convert or enclosing type
so, trying achieve here not possible.
best option extension method on int
performs conversion , returns string
.
Comments
Post a Comment