I have a class as follows:
public class Document
{
public List<DocumentSection> sections = new List<DocumentSection>();
...
Various questions cover the situation where a property needs to be writable from inside the class but readonly from outside it (http://stackoverflow.com/questions/4662180/c-sharp-public-variable-as-writeable-inside-the-clas-but-readonly-outside-the-cl)
I'd like to do the same but for this collection - allow adding to it from within the class, but only allow a user to iterate through it when they are outside it. Is this elegantly doable?
Thanks
Expose the collection as IEnumerable
so that users can only iterate through it.
public class Document {
private List<DocumentSection> sections;
public IEnumerable<DocumentSection> Sections
{
get { return sections; }
}
}