user1765862
user1765862

Reputation: 14155

Reversing simple string array

I'm trying to reverse string elements inside array, but I'm getting exact string order. Here's the code

public static void ReverseArrayElements()
        {
            Console.WriteLine("This will init array elements and reverse its elements");
            string[] elements = { "one, two, three" };
            for (int i = 0; i < elements.Length; i++)
            {
                Console.WriteLine("Array elements before reversing: {0}", elements[i]);    
            }
            Console.WriteLine("\n");

            Array.Reverse(elements);

            for (int i = 0; i < elements.Length; i++)
            {
                Console.WriteLine("Array elements after reversing: {0}", elements[i]);    
            }
            Console.ReadLine();
        }

Result is one, two, three and after sorting again one, two, three.

What am I doing wrong here?

Upvotes: 1

Views: 5847

Answers (2)

Hayden O&#39;Sullivan
Hayden O&#39;Sullivan

Reputation: 51

You can usea Array.Reverse (yourarray)

Upvotes: 2

dwjohnston
dwjohnston

Reputation: 11860

You've created an array of one element, containing the string "one, two, three".

Instead, you want to intialise that array with:

        string[] elements = { "one", "two", "three" };

Upvotes: 9

Related Questions