Reverse array in C#

To reverse an array we can use Reverse method of Array class.

Overloads

This method has two overloads.
First overload takes an array as parameter which will be reversed.
Second overload takes 3 parameters. First is array second is start index from where to start and third is length.

string[]arr = new string[3];
arr[0] = "One";
arr[1] = "Two";
arr[2] = "Three";

Console.WriteLine("Array before reversing");
foreach(string str in arr)
{
    Console.WriteLine(str);
}
Array.Reverse(arr);
Console.WriteLine("Array after reversing");
foreach (string str in arr)
{
    Console.WriteLine(str);
}

Output

Array before reversing
One
Two
Three
Array after reversing
Three
Two
One

            

Comments

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Using Progress Bar In C#

Jquery serer side datatables in asp.net