title: Adding to List inside a Using block
body: <p>I have a class which implements IDisposable like such </p>
  
  <pre><code>  public class SomeClass : IDisposable
  {
      private IList&lt;string&gt; _someList = new List&lt;string&gt;();
  
      public IList&lt;string&gt; SomeList
      {
          get { return _someList; }
      }
  
      public void Dispose()
      {
          _someList = null;
      }
  }
  </code></pre>
  
  <p>and a method that uses 'using' blocks to create two instances of this class and add it to a collection like such </p>
  
  <pre><code>private static void Main(string[] args)
      {
          IList&lt;SomeClass&gt; classes = new List&lt;SomeClass&gt;();
  
          using (var sc = new SomeClass())
          {
              sc.SomeList.Add("a");
              classes.Add(sc);
          }
  
          using (var sc = new SomeClass())
          {
              sc.SomeList.Add("b");
              classes.Add(sc);
          }
  
          foreach (var a in classes)
          {
              Console.WriteLine(a.SomeList[0]);
          }
  
          Console.ReadLine();
      }
  </code></pre>
  
  <p>Whats happening here is by the time i come to the foreach to iterate through my elements in "classes" the "SomeList" property of both my objects are null. </p>
  
  <p>I do understand that since each using executes Dispose() and in the dispose, i AM null-ing the two lists, they will be null.</p>
  
  <p>My question is how do i achieve this without having to stop null-ing "SomeList" inside my dispose().</p>
  
  <p>thanks</p>
  
  <p>--UPDATE 1</p>
  
  <p>Something closer to real code </p>
  
  <pre><code>base.OnPreRender(e);
  
      Page page = HttpContext.Current.CurrentHandler as Page;
      HtmlHead head = (HtmlHead)page.Header;
  
      if (Settings.AreSet)
      {
          using (var noIndex = new HtmlMeta())
          {
              noIndex.Name = "somename";
              noIndex.Content = "somecontent";
  
              head.Controls.AddAt(0, noIndex);
          }
      }
  
      using (var machineName = new HtmlMeta())
      {
          machineName.Name = "somename2";
          machineName.Content = "somecontent2";
  
          head.Controls.AddAt(1, machineName);
      }
  
      UpdateHeader(head);
  </code></pre>
  
  <p>--UPDATE #2 
  A variant of the above class with a string property now, as opposed to a a list. </p>
  
  <pre><code>public class SomeClass2 : IDisposable
  {
      public string SomeString { get; set; }
  
      public void Dispose()
      {
          SomeString = string.Empty;
      }
  }
  </code></pre>
  
  <p>It still does the same thing for the following method. My 'classes' contains lists of 'SomeClass2' which has 'SomeString' as empty (what i set in my dispose())</p>
  
  <pre><code>IList&lt;SomeClass2&gt; classes = new List&lt;SomeClass2&gt;();
  
          using (var sc = new SomeClass2())
          {
              sc.SomeString = "a";
              classes.Add(sc);
          }
  
          using (var sc = new SomeClass2())
          {
              sc.SomeString = "a";
              classes.Add(sc);
          }
  
          foreach (var a in classes)
          {
              Console.WriteLine(a.SomeString);
          }
  
  
          Console.ReadLine();
  </code></pre>