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:
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
List< int > firstlist = new List< int >();
for ( int i = 1; i <= 5; i++) {
firstlist.Add(i);
}
Console.WriteLine( "Elements Present in List:" );
foreach ( int k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine( " " );
Console.WriteLine( "After Reversing: " );
firstlist.Reverse();
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:
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
List< string > firstlist = new List< string >();
firstlist.Add( "Geeks" );
firstlist.Add( "C#" );
firstlist.Add( "Java" );
firstlist.Add( "C++" );
Console.WriteLine( "Elements Present in List:" );
foreach ( string k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine( " " );
Console.WriteLine( "After Reversing: " );
firstlist.Reverse();
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:
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
List< string > firstlist = new List< string >();
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:" );
foreach ( string k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine( " " );
Console.WriteLine( "After Reversing: " );
firstlist.Reverse(2, 4);
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:
using System;
using System.Collections;
using System.Collections.Generic;
class Geeks {
public static void Main()
{
List< string > firstlist = new List< string >();
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:" );
foreach ( string k in firstlist)
{
Console.WriteLine(k);
}
Console.WriteLine( " " );
Console.WriteLine( "After Reversing: " );
firstlist.Reverse(-1, 4);
foreach ( string k in firstlist)
{
Console.WriteLine(k);
}
}
}
|
RUntimeError:
Unhandled Exception:
System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: index
Reference: