views:

171

answers:

2

In an interview question, I saw this. How inverse the content of this list without loose the references.

List<string> list = new List<string>() {"AA", "BB", "CC" };

Update :

The question is more like this, you have :

public void ReversedList<T>(IList<T> listToReverse)
{
 // To be implemented
}

sample the list has 1,2,3,4 and must return 4,3,2,1 without lose reference.

+8  A: 

Do you mean reverse, i.e. change the order to "CC", "BB", "AA"? Just call List<T>.Reverse():

list.Reverse();

If that's not what you mean, please edit your question to be more specific. (It's not really clear what you mean by "without loose the references" either.)

Jon Skeet
+2  A: 

Unless I'm missing something, list.Reverse() would do exactly what you want (reverse the elements in place rather than create a new list with identical items in the reverse order).

Justin Niessner