title: How to check if a generic parameter is not null without boxing
body: <p>I want to write a method that accepts a single <code>IBar</code> and call its <code>Baz</code> method.</p>
  
  <p>This throws when <code>obj</code> is null:</p>
  
  <pre><code>void Foo&lt;T&gt;(T obj)
      where T : IBar
      =&gt; obj.Baz();
  </code></pre>
  
  <p>This boxes <code>obj</code> when it is a value type:</p>
  
  <pre><code>void Foo&lt;T&gt;(T obj)
      where T : IBar
      =&gt; obj?.Baz();
  </code></pre>
  
  <p>This doesn't call <code>Baz</code> if <code>obj</code> is zero:</p>
  
  <pre><code>void Foo&lt;T&gt;(T obj)
      where T : IBar
  {
      if (!EqualityComparer&lt;T&gt;.Default.Equals(obj, default(T)))
          obj.Baz();
  }
  </code></pre>
  
  <p>And here, <code>Foo(new Bar())</code> always picks the generic method no matter if <code>Bar</code> is a class or struct:</p>
  
  <pre><code>void Foo&lt;T&gt;(T obj)
      where T : struct, IBar
      =&gt; obj.Baz();
  
  void Foo(IBar obj)
      =&gt; obj?.Baz();
  </code></pre>
  
  <p>This makes my eyes bleed:</p>
  
  <pre><code>void FooVal&lt;T&gt;(T obj)
      where T : struct, IBar
      =&gt; obj.Baz();
  
  void FooRef(IBar obj)
      =&gt; obj?.Baz();
  </code></pre>
  
  <p>So is there a best practice for this? I'm open to all suggestions.</p>
  
  <p>Edit:</p>
  
  <p>The question is marked as duplicate with <a href="https://stackoverflow.com/q/2974519/704144">Generic constraints, where T : struct and where T : class</a> and with the old title, it was. So I've updated the title to convey my problem better. What I'm asking is that how can I call a generic method and use the argument <em>only if it is not null, without boxing.</em></p>
  
  <p>Some workarounds explained for the linked question <em>may be used</em> to answer this one but I still believe this is a fundamentally different question.</p>