Introduction
In this snippet we will write a program which reverses an array with out using any in-built C#.NET function. Basically it shifts all elements from an array from left side to right side.
Example: If input array is {1,2,3,4,5,6} , we should print {6,5,4,3,2,1}
using System;
namespace DotNetMirror
{
class ArrayReverse
{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 4, 5, 6 };
int startelement = 0; //start index will be 0
int endelement = array.Length - 1; //end index will be length-1
while (startelement < endelement)
{
int temp = array[endelement]; //store end element value in temporary variable for swapping
array[endelement] = array[startelement];
array[startelement] = temp;
startelement++;
endelement--;
}
Console.WriteLine("Result: {0}", string.Join("", array));
Console.ReadKey();
}
}
}
Output: