string[] _myStrings = { "Hello", "There", "Happy", "Day" };
public IEnumerable<string> MyStrings1
{
get
{
return new System.Collections.ObjectModel.ReadOnlyCollection<string>(_myStrings);
}
}
public IEnumerable<string> MyStrings2
{
get
{
return from s in _myStrings select s;
}
}
I have seen some discussion about not using arrays for public properties.
I have been using the MyStrings2 Convention. Is there some reason I should be using MyStrings1 instead?
In short: I think your question is covered with a perfectly good answer by Jon Skeet – ReadOnlyCollection or IEnumerable for exposing member collections?
In addition:
You can just emulate
AsReadOnly():UPDATE:
This doesn’t create a copy of
list.ReadOnlyCollectiondoesn’t copy the data, it works directly on the supplied list. See documentation: