title: Strange exception thrown when defining an expression with == between a generic struct type to its nullable counterpart
body: <p><strong>Final update:</strong> See this <a href="https://stackoverflow.com/questions/16797890/why-are-generic-and-non-generic-structs-treated-differently-when-building-expres">new question</a> that narrows the problem down to generic structs.</p>
  
  <hr>
  
  <p>I have some code that is building an <code>Expression&lt;Func&lt;..&gt;&gt;</code> that compares a value type to the nullable of the same value type. In the line that defines the expression, I'm getting the following <code>InvalidOperationException</code>:</p>
  
  <blockquote>
    <p>The operands for operator 'Equal' do not match the parameters of method 'op_Equality'.</p>
  </blockquote>
  
  <p>Any explanation for this?</p>
  
  <p><strong>Note</strong>: None of the overriden methods are ever called, the exception is thrown when the expression is being built by .NET</p>
  
  <p>Here's the full code to reproduce the exception:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Linq.Expressions;
  
  namespace ConsoleApplication1
  {
      struct MyStruct&lt;T&gt; : IEquatable&lt;MyStruct&lt;T&gt;&gt;
          where T : struct
      {
          private readonly T _value;
  
          public MyStruct(T val) { this._value = val; }
  
          public override bool Equals(object obj)
          {
              return false;
          }
  
          public override int GetHashCode()
          {
              return base.GetHashCode();
          }
  
          public static bool operator ==(MyStruct&lt;T&gt; a, MyStruct&lt;T&gt; b)
          {
              return false;
          }
  
          public static bool operator !=(MyStruct&lt;T&gt; a, MyStruct&lt;T&gt; b)
          {
              return false;
          }
  
          public bool Equals(MyStruct&lt;T&gt; other)
          {
              return false;
          }
      }
  
      class Program
      {
          static void Main(string[] args)
          {
              Expression&lt;Func&lt;MyStruct&lt;int&gt;, Nullable&lt;MyStruct&lt;int&gt;&gt;, bool&gt;&gt; expr = 
                  (value, nullableValue) =&gt; value == nullableValue;
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p><strong>Update</strong>: Greatly simplified the code that reproduces the exception</p>
  
  <hr>
  
  <p><strong>Also</strong>: Note that using a Func instead of an expression does not cause this exception:</p>
  
  <pre><code>Func&lt;MyStruct&lt;int&gt;, Nullable&lt;MyStruct&lt;int&gt;&gt;, bool&gt; func =
      (value, nullableValue) =&gt; value == nullableValue;
  
  func(new MyStruct&lt;int&gt;(), null);
  </code></pre>
  
  <p>The above runs with no errors.</p>
  
  <hr>
  
  <p><strong>Update 3</strong>: Seems that removing the <code>IEquatable&lt;&gt;</code> interface doesn't prevent the exception, so the code can be further simplified.</p>