Encapsulate Collections

If an object has a collection – e.g. a List<> – other code should not be able to manipulate that collection directly.

Instead the collection should be a private field and everything that’s done with it should be done by the encapsulating class.

Even if some other code needs to go through the whole collection, the encapsulating class should not provide the collection itself. That would allow another class to add to the list, remove from it etc. Instead we should only provide a copy the collection (for example with the .ToList() function provided by LINQ).