You would expose it as IEnumerable<T> , but not just return it directly:
public IEnumerable<object> Objects { get { return obs.Select(o => o); } }
Since you indicated that you only want to traverse the list, that’s all you need.
You might be tempted to return the List<object> directly as IEnumerable<T> , but that would be wrong because you could easily check the IEnumerable<T> at runtime, determine that it is a List<T> , and pass it so mutate the contents.
However, using return obs.Select(o => o); , you are returning an iterator over List<object> , not a direct link to List<object> .
Some might think that this qualifies as a “degenerate expression” in accordance with section 7.15.2.5 of the C # Language Specification. However, Eric Lippert talks in detail about why this forecast is not optimized .
In addition, people suggest using the AsEnumerable extension method . This is incorrect because the reference identifier of the source list is saved. In the "Notes" section of the documentation:
The AsEnumerable<TSource>(IEnumerable<TSource>) method has no effect other than changing the type of the compilation time source from a type that implements IEnumerable<T> to IEnumerable<T> .
In other words, all he does is specify the original parameter IEnumerable<T> , which does not protect referential integrity, the original link is returned and can be dropped to List<T> and used to change the list.
casperOne
source share