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.