Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - REVERSE A LIST


How to reverse a List?


Reversal of a List can be done using Reverse() method. It is independent of the alphabets. And is not a sort. It is just a reversal.


Example :



using System.Collections.Generic;
    
public class MyApplication
{
    public static void Main(string[] args)
    {
        var x = new List<string>(){"Mohan", "Kriti", "Salim"};
        x.Reverse();
        foreach (var data in x)  
        {  
            System.Console.WriteLine(data);  
        }
    }    
}


Output :



  Salim
  Kriti
  Mohan

So, in the above code we have created a List and initialised to the variable x.


var x = new List<string>(){"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

C_Sharp

Then we have used the Reverse() Method to reverse the elements of the List x.


x.Reverse();

And the List x gets sorted in reverse order with Salim as the first value, Mohan second and Kriti as the third.

C_Sharp

And we get the below output.

Output :



  Salim
  Kriti
  Mohan