In the exercise Generic Catalog add a method GetAll() which returns all the elements in the catalog.
This method is dangerous since it returns a reference to a private data structure inside the Catalog class. With this reference an evil programmer can do anything he likes to the private data structure.
Return an unmodifiable version of the private datastructure - or a copy of the private data structure.
In this exercise you will try different ways to make unmodifiable or copied collecions.
Make another method to the Catalog class
public IReadOnlyCollection<TElement> GetAllReadOnlyCollection()
This method must return an unmodifiable version of the elements in the catalog.
Hint: Use the class ReadOnlyCollection
Make yet another method to the Catalog class
public ICollection<TElement> GetAllAsReadOnly()
This method must return a read-only version of the collection. If the client calls a modyfing method like Add(...) the client gets an exception
Hint: Use the method list.AsReadOnly()
Unit testing: Yes
Make yet another method to the Catalog class
public ICollection<TElement> GetAllCopy()
This methodwhich return a copy of the elements in the catalog.
Hint: The copy constructor of the class Collection<T>(IList<T> list) is a very thin wrapper of the list. Not suitable for this job.
Hint2: Do you own hand copying: Make a Collection<T> object, loop through all elements in the original collection and add the elements to the new Collection object.
Make yet another method
public IEnumerable<TElement> GetAllEnumerable()
Since IEnumerable<T> is a super-type of ICollection<T>, and hence of IList<T>, ISet<T>, etc. you can just return the collection as it is.
The evil programmer might do some nasty typecasting to get the original type back.
IEnumerable<T> all = catalog.getAll();
ICollection<String> coll = all as ICollection<String>;
Now the evil programmer can do much too much to the coll object.