Is there a good way to enumerate through only a subset of a Collection in C#? That is, I have a collection of a large number of objects (say, 1000), but I'd like to enumerate through only elements 250 - 340. Is there a good way to get an Enumerator for a subset of the collection, without using another Collection?
Edit: should have mentioned that this is using .NET Framework 2.0.
You might be able to do something with Linq. The way I would do this is to put the objects into an array, then I can choose which items I want to process based on the array id.
Adapting Jared's original code for .Net 2.0:
And to use it:
I like to keep it simple (if you don't necessarily need the enumerator):
If you find that you need to do a fair amount of slicing and dicing of lists and collections, it might be worth climbing the learning curve into the C5 Generic Collection Library.
Adapting Jarad's code once again, this extention method will get you a subset that is defined by item, not index.
Try the following
Or more generally
EDIT 2.0 Solution