title: C# 5.0 ForEach invalid args error when using extension method in lambda
body: <p>I have encounted strange behaviour for for foreach method. When I put down this code:</p>
  <pre><code>var result = repository.Data.Where(&lt;some_long_selector&gt;).ToList();
  result.ForEach(e =&gt;
  {
      e.x = CalcX(e);
      e.y = GetAllYs().FirstOrDefault(y =&gt; y.Id == e.x)
  });
  </code></pre>
  <p>everything all Ok.</p>
  <p>But when I try to use homebrew elvis &quot;operator&quot; (or actually &quot;default value operator&quot;) like this :</p>
  <pre><code>var result = repository.Data.Where(&lt;some_long_selector&gt;).ToList();
  result.ForEach(e =&gt;
  {
      e.x = CalcX(e);
      e.y = GetAllYs().FirstOrDefault(y =&gt; y.Id == e.x).Elvis(new Y(&quot;empty&quot;));
  });
  </code></pre>
  <p>where <code>Elvis</code> is</p>
  <pre><code>public static T Elvis&lt;T&gt;(this T? instance, T defaultValue)
  {
      if (instance == null) return defaultValue;
      else return instance.Value;
  }
  </code></pre>
  <p>I am getting &quot;invalid args&quot; error for ForEach method (i.e. some type error). Which is quite unexpected for me. I understand possible workarounds, but I just want to know how and why this happens and what is the least effort way to correct it, while keeping this (or slightly modified) Elvis extension function?</p>
  <p>P.S. I am Java developer who need to support some legacy C# code while our team developing replacement for it. So, sorry, if I miss something obvious</p>