Returning a ReadOnlyCollection from a method with an IList return type
- by devoured elysium
Here I have the following bit of code:
private IList<IState> _states = new List<IState>();
private ReadOnlyCollection<IState> _statesViewer;
public IList<IState> States { get { return _statesViewer; } }
Generally it is preferable to return interfaces than the concrete classes themselves, but in this case, shouldn't I set as the return type of the States property a ReadOnlyCollection?
Any user of my library will think it is possible to anything you can do with an IList if I set it as so, and that means adding elements. That is not true and I'm definitely breaking the contract exposing it as an IList.
Am I right with this view or there is something else I am missing here?