What is the closest way of writing a C++ std::reverse equivalent in C#?

Viewed 235

The reverse algorithm in the C++ standard library is equivalent to

template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
  while ((first!=last)&&(first!=--last)) {
    std::iter_swap (first,last);
    ++first;
  }
}

according to http://www.cplusplus.com/reference/algorithm/reverse/. I want to write the equivalent in C#:

public void Reverse<T> ( T first, T last ) 
{
    // ... 
}

First of all, how do you pass in two references to IEnumerators, with the second being one that can travel backwards? Is there a natural way of doing this or does it require first extending IEnumerable<T>?

Second of all, if the answer to the previous question is no, then is there a Skeet-certified C# way of writing reverse that equals the C++ one in terms of generality and effeciency? If so, what is it?

1 Answers
Related