title: Operator Overloading with Interface-Based Programming in C#
body: <h2>Background</h2>
  
  <p>I am using interface-based programming on a current project and have run into a problem when overloading operators (specifically the Equality and Inequality operators).</p>
  
  <hr>
  
  <h2>Assumptions</h2>
  
  <ul>
  <li>I'm using C# 3.0, .NET 3.5 and Visual Studio 2008</li>
  </ul>
  
  <p>UPDATE - The Following Assumption was False!</p>
  
  <ul>
  <li>Requiring all comparisons to use Equals rather than operator== is not a viable solution, especially when passing your types to libraries (such as Collections).</li>
  </ul>
  
  <p>The reason I was concerned about requiring Equals to be used rather than operator== is that I could not find anywhere in the .NET guidelines that it stated it would use Equals rather than operator== or even suggest it. However, after re-reading <a href="http://msdn.microsoft.com/en-us/library/ms173147.aspx" rel="noreferrer">Guidelines for Overriding Equals and Operator==</a> I have found this:</p>
  
  <blockquote>
    <p>By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. It is not a good idea to override operator == in non-immutable types.</p>
  </blockquote>
  
  <p>and this <a href="http://msdn.microsoft.com/en-us/library/ms131187.aspx" rel="noreferrer">Equatable Interface</a></p>
  
  <blockquote>
    <p>The IEquatable interface is used by generic collection objects such as Dictionary, List, and LinkedList when testing for equality in such methods as Contains, IndexOf, LastIndexOf, and Remove. It should be implemented for any object that might be stored in a generic collection.</p>
  </blockquote>
  
  <hr>
  
  <h2>Contraints</h2>
  
  <ul>
  <li>Any solution must not require casting the objects from their interfaces to their concrete types.</li>
  </ul>
  
  <hr>
  
  <h2>Problem</h2>
  
  <ul>
  <li>When ever both sides of the operator== are an interface, no operator== overload method signature from the underlying concrete types will match and thus the default Object operator== method will be called.</li>
  <li>When overloading an operator on a class, at least one of the parameters of the binary operator must be the containing type, otherwise a compiler error is generated (Error BC33021 <a href="http://msdn.microsoft.com/en-us/library/watt39ff.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/watt39ff.aspx</a>)</li>
  <li>It's not possible to specify implementation on an interface</li>
  </ul>
  
  <p>See Code and Output below demonstrating the issue.</p>
  
  <hr>
  
  <h2>Question</h2>
  
  <p><strong>How do you provide proper operator overloads for your classes when using interface-base programming?</strong></p>
  
  <hr>
  
  <h2>References</h2>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/53k8ybth.aspx" rel="noreferrer">== Operator (C# Reference)</a></p>
  
  <p>For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.</p>
  
  <hr>
  
  <h2>See Also</h2>
  
  <hr>
  
  <h2>Code</h2>
  
  <pre><code>using System;
  
  namespace OperatorOverloadsWithInterfaces
  {
      public interface IAddress : IEquatable&lt;IAddress&gt;
      {
          string StreetName { get; set; }
          string City { get; set; }
          string State { get; set; }
      }
  
      public class Address : IAddress
      {
          private string _streetName;
          private string _city;
          private string _state;
  
          public Address(string city, string state, string streetName)
          {
              City = city;
              State = state;
              StreetName = streetName;
          }
  
          #region IAddress Members
  
          public virtual string StreetName
          {
              get { return _streetName; }
              set { _streetName = value; }
          }
  
          public virtual string City
          {
              get { return _city; }
              set { _city = value; }
          }
  
          public virtual string State
          {
              get { return _state; }
              set { _state = value; }
          }
  
          public static bool operator ==(Address lhs, Address rhs)
          {
              Console.WriteLine("Address operator== overload called.");
              // If both sides of the argument are the same instance or null, they are equal
              if (Object.ReferenceEquals(lhs, rhs))
              {
                  return true;
              }
  
              return lhs.Equals(rhs);
          }
  
          public static bool operator !=(Address lhs, Address rhs)
          {
              return !(lhs == rhs);
          }
  
          public override bool Equals(object obj)
          {
              // Use 'as' rather than a cast to get a null rather an exception
              // if the object isn't convertible
              Address address = obj as Address;
              return this.Equals(address);
          }
  
          public override int GetHashCode()
          {
              string composite = StreetName + City + State;
              return composite.GetHashCode();
          }
  
          #endregion
  
          #region IEquatable&lt;IAddress&gt; Members
  
          public virtual bool Equals(IAddress other)
          {
              // Per MSDN documentation, x.Equals(null) should return false
              if ((object)other == null)
              {
                  return false;
              }
  
              return ((this.City == other.City)
                  &amp;&amp; (this.State == other.State)
                  &amp;&amp; (this.StreetName == other.StreetName));
          }
  
          #endregion
      }
  
      public class Program
      {
          static void Main(string[] args)
          {
              IAddress address1 = new Address("seattle", "washington", "Awesome St");
              IAddress address2 = new Address("seattle", "washington", "Awesome St");
  
              functionThatComparesAddresses(address1, address2);
  
              Console.Read();
          }
  
          public static void functionThatComparesAddresses(IAddress address1, IAddress address2)
          {
              if (address1 == address2)
              {
                  Console.WriteLine("Equal with the interfaces.");
              }
  
              if ((Address)address1 == address2)
              {
                  Console.WriteLine("Equal with Left-hand side cast.");
              }
  
              if (address1 == (Address)address2)
              {
                  Console.WriteLine("Equal with Right-hand side cast.");
              }
  
              if ((Address)address1 == (Address)address2)
              {
                  Console.WriteLine("Equal with both sides cast.");
              }
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <h2>Output</h2>
  
  <pre><code>Address operator== overload called
  Equal with both sides cast.
  </code></pre>