简体   繁体   中英

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 ;-)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM