I have an ICollection<T>
called foos
in my class which I want to expose as read-only (see this question). I see that the interface defines a property .IsReadOnly
, which seems appropriate... My question is this: how do I make it obvious to the consumer of the class that foos
is read-only?
I don't want to rely on them remembering to query .IsReadOnly
before trying a not-implemented method such as .Add()
. Ideally, I would like to expose foos
as a ReadOnlyCollection<T>
, but it does not implement IList<T>
. Should I expose foo
via a method called, for example, GetReadOnlyFooCollection
rather than via a property? If so, would this not confuse someone who then expects a ReadOnlyCollection<T>
?
This is C# 2.0, so extension methods like ToList()
are not available...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…