How to compare System.Enum to enum (implementation) without boxing? : <p>How can I compare a <code>System.Enum</code> to an <code>enum</code> without boxing? For example, how can I make the following code work without boxing the <code>enum</code>?</p>

<pre><code>enum Color
{
    Red,
    Green,
    Blue
}

...

System.Enum myEnum = GetEnum(); // Returns a System.Enum. 
                                // May be a Color, may be some other enum type.

...

if (myEnum == Color.Red) // ERROR!
{
    DoSomething();
}
</code></pre>

<p>To be specific, the intent here is not to compare the underlying values. In this case, the underlying values are not meant to matter. Instead, if two Enums have the same underlying value, they should not be considered equal if they are two different kinds of enums:</p>

<pre><code>enum Fruit
{
    Apple = 0,
    Banana = 1,
    Orange = 2
}

enum Vegetable
{
    Tomato = 0,
    Carrot = 1,
    Celery = 2
}

myEnum = Vegetable.Tomato;
if (myEnum != Fruit.Apple) // ERROR!
{
    // Code should reach this point 
    // even though they're the same underlying int values

    Log("Works!");
}
</code></pre>

<p>This is basically the same functionality as <code>Enum.Equals(Object)</code>. Unfortunately <code>Equals()</code> requires boxing the enum, which in our case would be a naughty thing to do.</p>

<p>Is there a nice way to compare two arbitrary enums without boxing or otherwise creating a bunch of overhead?</p>

<p>Thanks for any help!</p>
