Possible Duplicate:
When are two enums equal in C#?
I have the following classes as part of a simple state machine.
Please note that all generic type parameters HAVE to be an enumeration. That has been enforced in the constructors (not shown here).
// Both [TState] and [TCommand] will ALWAYS be enumerations.
public class Transitions<TState, TCommand>: List<Transition<TState, TCommand>>
{
public new void Add (Transition<TState, TCommand> item)
{
if (this.Contains(item))
throw (new InvalidOperationException("This transition already exists."));
else
this.Add(item);
}
}
// Both [TState] and [TCommand] will ALWAYS be enumerations.
public class Transition<TState, TCommand>
{
TState From = default(TState);
TState To = default(TState);
TCommand Command = default(TCommand);
}
public sealed class TransitionComparer<TState>:
IComparer<TState>
{
public int Compare (TState x, TState y)
{
int result = 0;
// How to compare here since TState is not strongly typed and is an enum?
// Using ToString seems silly here.
result |= x.From.ToString().CompareTo(y.From.ToString());
result |= x.To.ToString().CompareTo(y.To.ToString());
result |= x.Command.ToString().CompareTo(y.Command.ToString());
return (result);
}
}
The above does compile but I’m not sure if this is the right way to handle enums that have been passed in as generic type parameters.
Note: The compare function does not need to keep ordering in mind. Rather it needs to check for exact duplicates.
In that case, you shouldn’t be implementing
IComparer<T>. You should be implementingIEqualityComparer<T>– or, more simply, makeTransitionimplementIEquatable<Transition>.Note that you’re currently not using the
TransitionComparerin the rest of the code, as far as we can see. It sounds like you shouldn’t really need to write your own comparison code for each enum value – you’re just trying to combine them. Unfortunately enums don’t implementIEquatable<T>, which makes is a bit harder to do this without boxing – how performance critical is this?Here’s a sample equality implementation for
Transition:EDIT: To avoid boxing for equality, I suspect you would either need some kind of delegate to get at the underlying value (and if you need to support values with different underlying types, that’s pretty painful) or potentially use something like Unconstrained Melody which uses IL rewriting to enforce the constraint at compile time, and allows you to check for equality based on underlying value more efficiently.