Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You would expose it as an <code>IEnumerable&lt;T&gt;</code>, but not just returning it directly:</p> <pre><code>public IEnumerable&lt;object&gt; Objects { get { return obs.Select(o =&gt; o); } } </code></pre> <p>Since you indicated you only wanted <em>traversal</em> of the list, this is all you need.</p> <p>One might be tempted to return the <code>List&lt;object&gt;</code> directly as an <code>IEnumerable&lt;T&gt;</code>, but that would be incorrect, because one could easily inspect the <code>IEnumerable&lt;T&gt;</code> at runtime, determine it is a <code>List&lt;T&gt;</code> and cast it to such and mutate the contents.</p> <p>However, by using <code>return obs.Select(o =&gt; o);</code> you end up returning an iterator over the <code>List&lt;object&gt;</code>, not a direct reference to the <code>List&lt;object&gt;</code> itself.</p> <p>Some might think that this qualifies as a "degenerate expression" according to section 7.15.2.5 of the C# Language Specification. However, <a href="http://blogs.msdn.com/ericlippert/archive/2008/05/12/trivial-projections-are-usually-optimized-away.aspx" rel="nofollow noreferrer">Eric Lippert goes into detail as to why this projection isn't optimized away</a>.</p> <p>Also, people are suggesting that one use the <a href="http://msdn.microsoft.com/en-us/library/bb335435.aspx" rel="nofollow noreferrer">AsEnumerable extension method</a>. This is incorrect, as the reference identity of the original list is maintained. From the Remarks section of the documentation:</p> <blockquote>The <code>AsEnumerable&lt;TSource&gt;(IEnumerable&lt;TSource&gt;)</code> method has no effect other than to change the compile-time type of source from a type that implements <code>IEnumerable&lt;T&gt;</code> to <code>IEnumerable&lt;T&gt;</code> itself.</blockquote> <p>In other words, all it does is cast the source parameter to <code>IEnumerable&lt;T&gt;</code>, which doesn't help protect referencial integrity, the original reference is returned and can be cast back to <code>List&lt;T&gt;</code> and be used to mutate the list.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload