Reputation: 375
Just an academical question: Is it possible to avoid int casting when comparing Enum to int?
int i = 0;
if(i == (int)MyEnum.Whatever)
{
}
I would like to overload == operator in such a manner:
public static MyEnum operator ==(int lhs, MyEnum rhs)
{}
Thanks for reading ;-)
Upvotes: 2
Views: 2048
Reputation: 164331
You can't. See this similar question. As suggested in that question, you could define an extension method to do the comparison, in order to get rid of the repeated casts.
Upvotes: 1
Reputation: 62127
No, not possible. the question is - why do you compare to in at all?
Upvotes: 0