1

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?

2 Answers 2

9

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" };
Sign up to request clarification or add additional context in comments.

Comments

2

You can usea Array.Reverse (yourarray)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.