title: How to allow a generic type parameter for a C# method to accept a null argument?
body: <pre><code>private static Matcher&lt;T&gt; EqualTo&lt;T&gt;(T item)
  {
      return new IsEqual&lt;T&gt;(item);
  }
  </code></pre>
  
  <p>How do I modify the above method definition such that the following are valid/allowed.</p>
  
  <pre><code>EqualTo("abc");
  EqualTo(4);
  EqualTo(null); // doesn't compile. EqualTo&lt;string&gt;(null) does
  </code></pre>
  
  <p>Trying to port some Java code where <code>null</code> seems to be acceptable value for a <code>T</code> parameter.</p>
  
  <hr>
  
  <p><strong>Update</strong><br>
  Thanks: for all the answers - especially Eamon and Jason. I didn't want the method calls to bother with type-inference. The following overload fixed it.</p>
  
  <pre><code>    private static Matcher&lt;object&gt; EqualTo(object item)
      {
          return EqualTo&lt;object&gt;(item);
      }  
  </code></pre>
  
  <p>Actually the above question was a part of a larger puzzle. The end goal was for the following to work.      </p>
  
  <pre><code>        this.AssertThat(null, EqualTo(null));
          this.AssertThat(null, Not(EqualTo("hi")));
          this.AssertThat("hi", Not(EqualTo(null)));
  </code></pre>
  
  <p>Applied the same fix.. <strong>RFC</strong>. (Ignore the ugly extension method part - that's another problem. Wanted to have these methods in all test-fixtures without inheritance.)</p>
  
  <pre><code>public static void AssertThat&lt;T&gt;(this object testFixture, object actual, Matcher&lt;T&gt; matcher, string message = "")
  {
    AssertThat(anyObject, (T)actual, matcher, message);
  }
  
  public static void AssertThat&lt;T, TSuper&gt;(this object testFixture, T actual, Matcher&lt;TSuper&gt; matcher, string message = "") where T : TSuper
  {
    ... check and assert
  </code></pre>