Michal Krawiec
Michal Krawiec

Reputation: 375

Overloading enum operator C# and Silverlight

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

Answers (2)

driis
driis

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

TomTom
TomTom

Reputation: 62127

No, not possible. the question is - why do you compare to in at all?

Upvotes: 0

Related Questions