Reverse an Array items in C#

In this article I will explain that how to Reverse in Array.
  • 2621

The Array Reverse method is a static method on the Array type. Reverse method is invoked twice, and this reverses the original array, and then reverses the reversed array. Other overloaded method of Reverse () function takes object of array, an index and length of array. This function reverses the array from a particular index to the end.

namespace demo_array

{

    class Program

    {      

        static void Main(string[] args)

        {

            int i, j;

            int[] n1 = new int[10];

            int[] n2 = new int[10];

            for (i = 0; i < n1.Length; i++)n1[i] = i;

                Console.Write("Original Data: ");

            for (i = 0; i < n2.Length; i++)

            {

                Console.Write(n1[i] + " ");

            }

            Console.WriteLine();

            if (n2.Length >= n1.Length)

            {

                for (i = 0, j = n1.Length - 1; i < n1.Length; i++, j--)

                {

                    n2[j] = n1[i];

                }

            }

            Console.Write("Reversed Data: ");

            for (i = 0; i < n2.Length; i++)

            {

                Console.Write(n2[i] + " ");

            }

            Console.ReadKey();

        }

    }

}

Output

reverse_num_array.jpg

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.