title: C# coalesce operator doesn't replace a null method return value?
body: <p>I have this code:</p>
  
  <pre><code>MyClass _localMyClass = MyClassDAO.GetMyClassByID(123) ?? new MyClass();
  </code></pre>
  
  <p>This is the method:</p>
  
  <pre><code>    public static MyClass GetMyClassByID(int id)
      {
          var query = from m in ctx.MyClass
                      where m.MyClassID == id
                      select m;
  
          return query.FirstOrDefault&lt;MyClass&gt;();
      }
  </code></pre>
  
  <p>So the <code>FirstOrDefault()</code> doesn't find a hit in the entity framework context, which is the "ctx" object.  So the 'default' value to be returned is <code>null</code>, since the target of the query is a class.</p>
  
  <p>The result of the first bit of code, using the <code>??</code>, results in <code>_localMyClass</code> being what?  I would say it would be the <code>new MyClass()</code>.  Instead, <code>_localMyClass</code> ends up being null.  I tried grouping the logic with various sets of parentheses, but still no luck.</p>
  
  <p>Odder still; when I set a debug break point, and copy/paste the <code>MyClassDAO.GetMyClassByID(123) ?? new MyClass()</code> into the watch screen of Visual Studio, the result is the <code>new MyClass()</code> instead of <code>null</code>.</p>
  
  <p>Can anybody explain why it would be working in this manner?  Why it doesn't recognize the method return value as <code>null</code> and then instead use the <code>new</code> part?</p>