How do I overload an operator for an enumeration in C#?

Posted by ChrisHDog on Stack Overflow See other posts from Stack Overflow or by ChrisHDog
Published on 2009-08-31T04:41:57Z Indexed on 2010/04/10 19:23 UTC
Read the original article Hit count: 622

I have an enumerated type that I would like to define the >, <, >=, and <= operators for. I know that these operators are implictly created on the basis of the enumerated type (as per the documentation) but I would like to explictly define these operators (for clarity, for control, to know how to do it, etc...)

I was hoping I could do something like:

public enum SizeType
{
    Small = 0,
    Medium = 1,
    Large = 2,
    ExtraLarge = 3
}

public SizeType operator >(SizeType x, SizeType y)
{

}

But this doesn't seem to work ("unexpected toke") ... is this possible? It seems like it should be since there are implictly defined operators. Any suggestions?

© Stack Overflow or respective owner

Related posts about c#

Related posts about enums