Returning a ReadOnlyCollection from a method with an IList return type

Posted by devoured elysium on Stack Overflow See other posts from Stack Overflow or by devoured elysium
Published on 2010-04-26T06:12:04Z Indexed on 2010/04/26 6:13 UTC
Read the original article Hit count: 239

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET