Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I have a list which I want to reverse and get an array from that reversed list and how this reverse method works?

please help me.

Thank you.
Posted
Comments
Sandeep Mewara 23-May-12 10:29am    
Have you tried anything so far?

You have Revesre and ToArray methods in List<t>, what's the problem?

See: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^]

To see how it is implemented use reflector, enable .net source debugging in visual studio, or google for it's source code.
 
Share this answer
 
C#
List<string> dinosaurs = new List<string>();

dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Parasauralophus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Coelophysis");
dinosaurs.Add("Oviraptor");

string[] input = dinosaurs.Reverse().ToArray();
 
Share this answer
 
v2
Comments
Hai Qui 23-May-12 10:24am    
Thank you. how reverse method works could you please explain.
C#
List<string> list = new List<string>();
list.Add("C++");
list.Add("C#");
list.Add("Java");
list.Add("Objective-C");

list.Reverse();
string[] array = list.ToArray();
for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine(array[i]);
}
 
Share this answer
 
v2
From MSDN framework blog, came to know that .NET Framework 4.0 has the implementation as the method checks the parameter and then invokes TrySZReverse: this particular method is implemented in native code, not managed code. Because this internal call is native, it is likely heavily optimized for performance.

Most important, though, is that it is much simpler to use Array.Reverse than to implement a custom reversal algorithm. Not only does Array.Reverse use native code, but it also makes your program simpler; it is therefore a clear win in most contexts.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900