title: FirstOrDefault() method of System.linq.Enumerable namespace throwing NullReferenceException
body: <p>I have a static readonly collection of user defined object type which is initialized like below.</p>
  
  <pre><code>private static readonly List&lt;MyClass&gt; obj = new List&lt;MyClass&gt;();
  </code></pre>
  
  <p>Another public utility method fills this collection and return the collection back.</p>
  
  <pre><code>public static List&lt;MyClass&gt; GetMyClasses(){
  
  // some code for DB calls goes here
  // in a loop based on recordset, i fill the collection like below
  obj.Add(new MyClass(){// my class property settings based on DB values});
  
  // finally return the collection
  return obj;
  }
  </code></pre>
  
  <p>I am getting the 'object refrence not set to an instance of an object' on 'ac' object in below line.</p>
  
  <pre><code>var cl = TestClass.GetMyClasses().FirstOrDefault(ac =&gt; ac.name == "myname")
  </code></pre>
  
  <p>I would like to know why 'ac' object is NULL here, even if we the collection is empty, as per MSDN FirstOrDefault should return NULL for reference types if it does't find the match.</p>
  
  <p>Can somebody help me understand what i am missing here? The same code was working flawlessly for months before it started throwing exception today. The same code is working well even today on other server without issues.</p>
  
  <p>For security reasons i can't post the entire exception stack trace but, it is pointing to below IL code</p>
  
  <p>at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)</p>
  
  <p>Updating the question with sample similar code of the issue.</p>
  
  <pre><code>public static class DemoDataAccess
      {
          private static readonly List&lt;MyTestClass&gt; classes
              = new List&lt;MyTestClass&gt;();
  
          public static IList&lt;MyTestClass&gt; GetMyClasses()
          {
              using (var cnn = new SqlConnection(@"connection string goes here"))
              {
                  cnn.Open();
                  using (var cmd = new SqlCommand("SP Name", cnn))
                  {
                      cmd.CommandType = CommandType.StoredProcedure;
                      using (SqlDataReader rdr = cmd.ExecuteReader())
                      {
                          while (rdr.Read())
                          {
                              classes.Add(new MyTestClass()
                                         {
                                           Id = Convert.ToInt32(rdr["id"]),
                                           Name = Convert.ToString(rdr["name"])
                                         });
                          }
                      }
                  }
              }
  
              return classes;
          }
      }
  
  
  public class MyTestClass
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Consistenly exception is thrown on below line and stack trace is point to 'ac' instance.</p>
  
  <pre><code>var obj = DemoDataAccess
               .GetMyClasses()
               .FirstOrDefault(ac =&gt; ac.Name == "something");
  </code></pre>