Exposing types as a less capable type doesn’t prevent use as their real type
A great example of this is when internal lists are exposed as IEnumerable
properties, e.g.:
private readonly List<string> internalStrings = new List<string>();
public IEnumerable<string> AllStrings { get { return internalStrings; } }
You’d likely think nobody can modify internal strings.
Wrong!
It's just too easy:
((List<string>)x.AllStrings).Add("Hello");
Even AsEnumerable
won’t help as that’s a LINQ method that does nothing in this case :-(
You can use AsReadOnly
which creates a wrapper over the list that throws when you try and set anything however this provides a good pattern for doing similar things with your own classes should you need to expose a subset of internal structures if unavoidable.