title: IComparable with Enums sent as generic type parameters (C#)
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1683706/when-are-two-enums-equal-in-c">When are two enums equal in C#?</a>  </p>
  </blockquote>
  
  
  
  <p>I have the following classes as part of a simple state machine.</p>
  
  <p>Please note that all generic type parameters HAVE to be an enumeration. That has been enforced in the constructors (not shown here).</p>
  
  <pre><code>// Both [TState] and [TCommand] will ALWAYS be enumerations.
  public class Transitions&lt;TState, TCommand&gt;: List&lt;Transition&lt;TState, TCommand&gt;&gt;
  {
      public new void Add (Transition&lt;TState, TCommand&gt; 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&lt;TState, TCommand&gt;
  {
      TState From = default(TState);
      TState To  = default(TState);
      TCommand Command = default(TCommand);
  }
  
  public sealed class TransitionComparer&lt;TState&gt;:
      IComparer&lt;TState&gt;
  {
      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);
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Note: The compare function does not need to keep ordering in mind. Rather it needs to check for exact duplicates.</p>