Neither.
If you want an enumerator, and already have an underlying IEnumerable
type (List
implements IList
which extends IEnumerable
), then just return it like that:
public IEnumerator<SomeType> GetEnumerator ()
{
return _list.GetEnumerator();
}
Otherwise, if you actually need a list, i.e. random access using indexes, then return an IList
; and if you actually want to return its internal implementation type, i.e. List
, then you can just make it an accessible property. Note though, that a private setter does not prevent modifying the list (adding or removing items etc.). If you want that, return a read-only list instead:
public IList<SomeType> List
{
get { return _list.AsReadOnly(); }
}
Regarding yield
could you please show me real-proof usage of yield return?
yield return
is useful, when you actually have a generator, when you actually need to generate the next item. A simple example would be a random number generator which provides you with another random number, as long as you keep asking it. There is not necessarily an end to it, but you might not know the amount of numbers before starting.
Another common usage would be anything that retrieves the data from some external source. For example a list of items from a webservice. Before you don’t know how many items there are, and you don’t necessarily know how many items you actually want (as you might want to display it in an endless display, showing one at a time). In that case, you could do it like that:
IEnumerable<Item> GetItems()
{
while (Service.HasMorePages())
{
foreach (Item item in Service.GetNextPage())
{
yield return item;
}
}
yield break;
}
GetNextPage
would always return a list of N
items at a time, and you would get the next one whenever you want more items than you have already received.