views:

835

answers:

3

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?

+8  A: 

You can't do that. You can only provide overloaded operators for classes and structs you define -- and at least one of the parameters should be of type of the class or struct itself. That is, you can declare an overloaded addition operator that adds a MyClass to MyEnum but you can never do that with two MyEnum values.

Mehrdad Afshari
That is disapointing, how do they do that implictly then? It seemed like there wouldn't be a way, but I figured if you could do it implicitly then there should be a way to do it explictly. I guess not. Thanks for the information.
ChrisHDog
They don't. There's no *implicit* way either. You simply can't overload operators for enums.
Mehrdad Afshari
According to: http://msdn.microsoft.com/en-us/library/aa664726(VS.71).aspx ... "Every enumeration type implicitly provides the following predefined comparison operators:" ... I was just hoping there was a way to explicitly provide a comparison operator similarly. So not an overload operator exactly, but something simlar.
ChrisHDog
Aha. I thought you mean you can implement an implicit operator for enum. The key word in the statement you referred to is *predefined*. The thing is, you can't define any custom operator implementations for enums.
Mehrdad Afshari
+3  A: 

As Mehrdad says, you can't do that on the enum itself. You could however make a couple of extension methods that work on your enum. That will make it look like methods on the enum.

static bool IsLessThan(this SizeType first, SizeType second) {
}
Brian Rasmussen
A: 

As other mentioned before, one cannot override operators on Enums, but you can do it on struct. See an example below. Let me know if it helped:

public struct SizeType
{
    private int InternalValue { get; set; }

    public static readonly int Small = 0;
    public static readonly int Medium = 1;
    public static readonly int Large = 2;
    public static readonly int ExtraLarge = 3;

    public override bool Equals(object obj)
    {
        SizeType otherObj = (SizeType)obj;
        return otherObj.InternalValue.Equals(this.InternalValue);
    }

    public static bool operator >(SizeType left, SizeType right)
    {
        return (left.InternalValue > right.InternalValue);
    }

    public static implicit operator SizeType(int otherType)
    {
        return new SizeType
        {
            InternalValue = otherType
        };
    }
}

public class test11
{
    void myTest()
    {
        SizeType smallSize = SizeType.Small;
        SizeType largeType = SizeType.Large;
        if (smallSize > largeType)
        {
            Console.WriteLine("small is greater than large");
        }
    }
}
Boris Modylevsky