C# | Reverse the order of the elements in the entire List or in the specified range
List<T>.Reverse Method is used to reverse the order of the elements in the List<T> or a portion of it. There are two methods in the overload list of List<T>.Reverse Method as follows:
- Reverse()
- Reverse(Int32, Int32)
Reverse() Method
This method is used to reverse the order of the elements in the entire List<T>.
Syntax:
public void Reverse ();
Below programs illustrate the use of above discussed method:
Example 1:
// C# Program to reverse the order of // the elements in the entire List<T> using System; using System.Collections; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating an List<T> of Integers List< int > firstlist = new List< int >(); // Adding elements to List for ( int i = 1; i <= 5; i++) { firstlist.Add(i); } Console.WriteLine( "Elements Present in List:" ); // Displaying the elements of List foreach ( int k in firstlist) { Console.WriteLine(k); } Console.WriteLine( " " ); Console.WriteLine( "After Reversing: " ); // using method Reverse() firstlist.Reverse(); // Displaying the elements of List foreach ( int k in firstlist) { Console.WriteLine(k); } } } |
Output:
Elements Present in List: 1 2 3 4 5 After Reversing: 5 4 3 2 1
Example 2:
// C# Program to reverse the order of // the elements in the entire List<T> using System; using System.Collections; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating an List<T> of Integers List< string > firstlist = new List< string >(); // Adding elements to List firstlist.Add( "Geeks" ); firstlist.Add( "C#" ); firstlist.Add( "Java" ); firstlist.Add( "C++" ); Console.WriteLine( "Elements Present in List:" ); // Displaying the elements of List foreach ( string k in firstlist) { Console.WriteLine(k); } Console.WriteLine( " " ); Console.WriteLine( "After Reversing: " ); // using method Reverse() firstlist.Reverse(); // Displaying the elements of List foreach ( string k in firstlist) { Console.WriteLine(k); } } } |
Output:
Elements Present in List: Geeks C# Java C++ After Reversing: C++ Java C# Geeks
Reverse(Int32, Int32) Method
This method is used to reverse the order of the elements in the specified range.
Syntax:
public void Reverse (int index, int count);
Parameters:
index: It is the zero-based starting index of the range which is to be reversed.
count: It is the number of elements in the range which is to be reversed.
Exceptions:
- ArgumentOutOfRangeException: If the index or count is less than zero.
- ArgumentException: If the index and count do not denote a valid range of elements in the List<T>.
Below programs illustrate the use of above discussed method:
Example 1:
// C# Program to reverse the order of // sub range in the List<T> using System; using System.Collections; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating an List<T> of Integers List< string > firstlist = new List< string >(); // Adding elements to List firstlist.Add( "1st" ); firstlist.Add( "2nd" ); firstlist.Add( "3rd" ); firstlist.Add( "4th" ); firstlist.Add( "5th" ); firstlist.Add( "6th" ); firstlist.Add( "7th" ); Console.WriteLine( "Elements Present in List:" ); // Displaying the elements of List foreach ( string k in firstlist) { Console.WriteLine(k); } Console.WriteLine( " " ); Console.WriteLine( "After Reversing: " ); // Reversing the sub-range // that starts from index 2 // i.e 3rd element, and // count is 4 elements firstlist.Reverse(2, 4); // Displaying the elements of List foreach ( string k in firstlist) { Console.WriteLine(k); } } } |
Output:
Elements Present in List: 1st 2nd 3rd 4th 5th 6th 7th After Reversing: 1st 2nd 6th 5th 4th 3rd 7th
Example 2:
// C# Program to reverse the order of // sub range in the List<T> using System; using System.Collections; using System.Collections.Generic; class Geeks { // Main Method public static void Main() { // Creating an List<T> of Integers List< string > firstlist = new List< string >(); // Adding elements to List firstlist.Add( "1st" ); firstlist.Add( "2nd" ); firstlist.Add( "3rd" ); firstlist.Add( "4th" ); firstlist.Add( "5th" ); firstlist.Add( "6th" ); firstlist.Add( "7th" ); Console.WriteLine( "Elements Present in List:" ); // Displaying the elements of List foreach ( string k in firstlist) { Console.WriteLine(k); } Console.WriteLine( " " ); Console.WriteLine( "After Reversing: " ); // taking negative index will give error firstlist.Reverse(-1, 4); // Displaying the elements of List foreach ( string k in firstlist) { Console.WriteLine(k); } } } |
RUntimeError:
Unhandled Exception:
System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: index
Reference:
Please Login to comment...