<p>I have a class property exposing an internal IList&lt;> through</p>

<pre><code>System.Collections.ObjectModel.ReadOnlyCollection&lt;&gt;
</code></pre>

<p>How can I pass a part of this <code>ReadOnlyCollection&lt;&gt;</code> without copying elements into a new array (I need a live view, and the target device is short on memory)? I'm targetting Compact Framework 2.0.</p>

[{'answer_id': 39460, 'author': 'Nir', 'author_id': 3509, 'author_profile': 'https://Stackoverflow.com/users/3509', 'pm_score': 1, 'selected': False, 'text': "<p>You can always write a class that implements IList and forwards all calls to the original list (so it doesn't have it's own copy of the data) after translating the indexes.</p>\n"}, {'answer_id': 39462, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 1, 'selected': False, 'text': '<p>You could use yield return to create a filtered list</p>\n\n<pre><code>IEnumerable&lt;object&gt; FilteredList()\n{\n    foreach( object item in FullList )\n    {\n        if( IsItemInPartialList( item )\n            yield return item;\n    }\n}\n</code></pre>\n'}, {'answer_id': 39464, 'author': 'Herms', 'author_id': 1409, 'author_profile': 'https://Stackoverflow.com/users/1409', 'pm_score': 0, 'selected': False, 'text': "<p>How do the filtered elements need to be accessed?  If it's through an Iterator then maybe you could write a custom iterator that skips the elements you don't want publicly visible?</p>\n\n<p>If you need to provide a Collection then you might need to write your own Collection class, which just proxies to the underlying Collection, but prevents access to the elements you don't want publicly visible.</p>\n\n<p>(Disclaimer: I'm not very familiar with C#, so these are general answers. There may be more specific answers to C# that work better)</p>\n"}, {'answer_id': 39467, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 5, 'selected': True, 'text': '<p>Try a method that returns an enumeration using yield:</p>\n\n<pre><code>IEnumerable&lt;T&gt; FilterCollection&lt;T&gt;( ReadOnlyCollection&lt;T&gt; input ) {\n    foreach ( T item in input )\n        if (  /* criterion is met */ )\n            yield return item;\n}\n</code></pre>\n'}, {'answer_id': 39469, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 1, 'selected': False, 'text': '<p>Depending on how you need to filter the collection, you may want to create a class that implements IList (or IEnumerable, if that works for you) but that mucks about with the indexing and access to only return the values you want. For example</p>\n\n<pre><code>class EvenList: IList\n{\n    private IList innerList;\n    public EvenList(IList innerList)\n    {\n         this.innerList = innerList;\n    }\n\n    public object this[int index]\n    {\n        get { return innerList[2*i]; }\n        set { innerList[2*i] = value; }\n    }\n    // and similarly for the other IList methods\n}\n</code></pre>\n'}, {'answer_id': 39481, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 3, 'selected': False, 'text': "<p>These foreach samples are fine, though you can make them much more terse if you're using .NET 3.5 and LINQ:</p>\n\n<pre><code>return FullList.Where(i =&gt; IsItemInPartialList(i)).ToList();\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/39447', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3205/']