In C++, anytime I want to process some range of items I do something like this:
template<class Iterator>
void method(Iterator range_begin, Iterator range_end);
Is there such a convenient way to do this in C#, not only for System.Array but for other collection classes as well?
I can’t think of any at the moment, but using LINQ you can easily create an
IEnumerable<T>representing a part of your collection.As I understand it, the C# way is to see a part of a collection as a collection itself, and make your
methodto work on this sequence. This idea allows the method to be ignorant to whether it’s working on the whole collection or its part.Examples:
etc.
Compare this to C++:
In C++, your code calculates iterators in order to pass them around, and mark the beginning and the end of your sequence. In C#, you pass lightweight
IEnumerables around. Due to the lazy evaluation this should have no overhead.