title: How to return a readonly copy of a collection
body: <p>I have a class that contains a collection. I want to provided a method or property that returns the contents of the collection. It's ok if calling classes can modify the individual objects but I do not want them adding or removing object from the actual collection. I have been copying all the objects to a new list, but now I'm thinking that I could just return the list as IEnumerable&lt;>.</p>
  
  <p>In the simplified example below is GetListC the best way to return a read only version of a collection?</p>
  
  <pre><code>public class MyClass
  {
      private List&lt;string&gt; mylist;
  
      public MyClass()
      {
          mylist = new List&lt;string&gt;();
      }
  
      public void Add(string toAdd)
      {
          mylist.Add(toAdd);
      }
  
      //Returns the list directly 
      public List&lt;String&gt; GetListA 
      { 
          get
              {
              return mylist;
              }
      }
  
      //returns a copy of the list
      public List&lt;String&gt; GetListB
      {
          get
          {
              List&lt;string&gt; returnList = new List&lt;string&gt;();
  
              foreach (string st in this.mylist)
              {
                  returnList.Add(st);
              }
              return returnList;
          }
      }
  
      //Returns the list as IEnumerable
      public IEnumerable&lt;string&gt; GetListC
      {
          get 
          {
              return this.mylist.AsEnumerable&lt;String&gt;();
          }
  
      }
  
  }
  </code></pre>