0

Why do I get an error at the following when trying to initialize arrChar2 to the Reverse of arrChar1 (error is at line arrChar2 initialization)?:

char[] arrChar1 = inputString.ToCharArray();

char[] arrChar2 = arrChar1.Reverse();
0

2 Answers 2

4

Because you are using the IEnumerable Reverse extensions that returns (in your context) an IEnumerable<char> not a char array.
If you want to get an array of chars, as output, you need to be explicit

char[] arrChar2 = arrChar1.Reverse().ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

Had to wait 10 min to accept your answer; please don't be the guy who downvoted me bc you though I didn't accept your answer.
@JeffOrris not me, but don't worry about it. Reps are fun but not the main reason to be here.
Steve 9 I use to care more..but am baffled at the downvote.
1

Probably the easiest way, as you don't need to create more variables:

using System;

 public class Program
 {
    public static void Main()
    {
        var inputString = "abcd";

        char[] arrChar1 = inputString.ToCharArray();

        Array.Reverse(arrChar1);

        Console.WriteLine(arrChar1);
    }
}

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.