title: C# Overloading Operator == and !=
body: <p>I am having problems getting the desired behavior out of these few classes and interfaces.</p>
  
  <p>Here is my problem,</p>
  
  <pre><code>//Inside a Unit Test that has access to internal methods and properties
  
  INode  firstNode, secondNode;
  
  INodeId  id = new NodeId (4);
  
  first = new Node (id, "node");
  second = new Node (id, "node");
  
  Assert.IsTrue (first == second);
  </code></pre>
  
  <p>The assert above is failing because it  seems to be going to the object class's equals method instead of the overloaded operator in the Node and NodeId classes.</p>
  
  <p>If you have any suggestions on how I can get the desired behavior, that would be awesome.</p>
  
  <p>Here is part of the Framework I am working on:</p>
  
  <pre><code>public interface IIdentifier&lt;T&gt; where T : class
  {
      TKeyDataType GetKey&lt;TKeyDataType&gt; ();
  
      bool Equals (IIdentifier&lt;T&gt; obj;
  }
  
  public interface INode
  {
      string name
      {
          get;
      }
  
      INodeId id
      {
          get;
      }
  }
  
  public interface INodeId : IIdentifier&lt;INode&gt;
  {
  }
  
  public class Node : INode
  {
      internal Node(INodeId  id, string name)
      { 
         //Work
      }
  
      public static bool operator == (Node n1, Node n2)
      {
          return n1.equals(n2);
      }
  
      public static bool operator != (Node n1, Node n2)
      {
          return !n1.equals(n2);
      }
  
      public bool Equals (INode  node)
      {
          return this.name == node.name &amp;&amp;
                 this.id = node.id;
      }
  
      #region INode Properties
  
      }
  
  public class NodeId : INodeId
  {
  
      internal NodeId(int id)
      { 
         //Work
      }
  
      public static bool operator == (NodeId  n1, NodeId  n2)
      {
          return n1.equals(n2);
      }
  
      public static bool operator != (NodeId  n1, NodeId  n2)
      {
          return !n1.equals(n2);
      }
  
      public override bool Equals (object obj)
      {
          return this.Equals ((IIdentifier&lt;INode&gt;) obj);
      }
  
      public bool Equals (IIdentifier&lt;INode&gt; obj)
      {
          return obj.GetKey&lt;int&gt;() ==  this.GetKey&lt;int&gt;();
      }
  
      public TKeyDataType GetKey&lt;TKeyDataType&gt; ()
      {
          return (TKeyDataType) Convert.ChangeType (
              m_id,
              typeof (TKeyDataType),
              CultureInfo.InvariantCulture);
      }
  
  
      private int m_id;
  
  }
  </code></pre>