This C# tutorial explains, how to reverse an array in C# without using the reverse function. In C#, the Array class provides a convenient Reverse
method that allows you to reverse an array with ease. But, in this tutorial, I will explain how to reverse an array in C# without using the Reverse
function.
Reverse an array in C# without using the Reverse
function
Now, let us check out different ways to reverse an array in C# without using the Reverse
function.
1. Using a Loop
One of the most straightforward methods is to use a loop. By swapping the elements of the array from the start and end progressively, you can reverse its order.
using System;
namespace ArrayReversal
{
class Program
{
static void Main()
{
string[] names = { "John", "Emily", "Michael", "Sarah", "George" };
ReverseArray(names);
foreach (var name in names)
{
Console.WriteLine(name);
}
}
static void ReverseArray<T>(T[] array)
{
int length = array.Length;
for (int i = 0; i < length / 2; i++)
{
T temp = array[i];
array[i] = array[length - i - 1];
array[length - i - 1] = temp;
}
}
}
}
Once you run the code using visual studio, you can see the output below:
2. Using Stacks
A stack is a Last In, First Out (LIFO) data structure. This makes it a natural choice for reversing arrays.
using System;
using System.Collections.Generic;
namespace StackArrayReversal
{
class Program
{
static void Main()
{
string[] USCities = { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" };
ReverseUsingStack(USCities);
foreach (var city in USCities)
{
Console.WriteLine(city);
}
}
static void ReverseUsingStack<T>(T[] array)
{
Stack<T> stack = new Stack<T>(array);
for (int i = 0; i < array.Length; i++)
{
array[i] = stack.Pop();
}
}
}
}
3. Using Recursion
Though recursion is generally more memory-intensive and can be a bit harder to grasp, it offers an elegant way to reverse an array.
using System;
namespace RecursiveArrayReversal
{
class Program
{
static void Main()
{
string[] USPresidents = { "Washington", "Adams", "Jefferson", "Madison", "Monroe" };
ReverseRecursively(USPresidents, 0, USPresidents.Length - 1);
foreach (var president in USPresidents)
{
Console.WriteLine(president);
}
}
static void ReverseRecursively<T>(T[] array, int start, int end)
{
if (start >= end) return;
T temp = array[start];
array[start] = array[end];
array[end] = temp;
ReverseRecursively(array, start + 1, end - 1);
}
}
}
Conclusion
While C#’s built-in Reverse
function provides an easy way to reverse arrays, understanding alternative methods can deepen your grasp of array manipulations and provide flexibility when you can’t rely on the built-in tools. Whether you’re using loops, stacks, or recursion, the key is to swap elements in the right order to achieve the desired reversal. I hope you got a complete idea of how to reverse an array in c# without reverse function.
You may like the following tutorials:
- How to reverse a string in C# using Linq?
- reverse a string in C# using inbuilt function
- How to reverse string in C# using while loop?
- How to Sort an Array in C# With Examples?
- How to Reverse a Number in C# Using For Loop?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…