0

I am trying to compile a c3 program but I keep on getting the error below.

Error CS1502: The best overloaded method match for string.Join(string, string[]) has some invalid arguments.

Error CS1503: Argument #2 cannot convert char[] expression to type string[]

for (int row = 0; row < 3; row++)
{
    char[] arr = new char[3];
    for (int col = 0; col < 3; col++)
    {
        if (board[row, col] == Player.None)
        {
            arr[col] = ' ';
        }
        else
        {
            arr[col] = board[row, col] == Player.P1 ? 'X' : 'O';
        }
    }
     
    Console.WriteLine("| {0} |", string.Join(" | ", arr));
2
  • 1
    Well everything is in the message : you're creating a char array while your method is expecting a string array... Commented Nov 14, 2013 at 10:01
  • Duplicate of How to convert a char array to a string array?. Commented Nov 14, 2013 at 10:03

3 Answers 3

3

The answer is very simple, arr is a char[] and not a string[].

try this

Console.WriteLine("| {0} |", string.Join(" | ", arr.Select(a => a.ToString())));
Sign up to request clarification or add additional context in comments.

Comments

2

You could either iterate the chars (as suggested by others) of the array or you could change the type of the array

for (int row = 0; row < 3; row++)
{
    var arr = new string[3];
    for (int col = 0; col < 3; col++)
    {
        if (board[row, col] == Player.None)
        {
            arr[col] = " ";
        }
        else
        {
            arr[col] = board[row, col] == Player.P1 ? "X" : "O";
        }
    }

    Console.WriteLine("| {0} |", string.Join(" | ", arr));
}

1 Comment

This is actually probably the best solution - using the right array type in the first place. Well done on looking at the problem fully rather than just fixing the erroring line (which is what I did in my head before others answered what I intended to).
0

As your arr is of type char[], you can use String(char[]) constructor to create string object instance

var strData= new string[]{new string(arr)};
Console.WriteLine("| {0} |", (string.Join(" | ", strData));

2 Comments

This will return | Foo | when the input is {'F', 'o', 'o'}, I suspect OP wants | F | o | o | in that case.
@RuneFS: Despite me upvoting your comment I just checked and it will accept a string fine. ALthough I've not looked at the docs this second I think it has a params string[] overload (or similar) that means a single string is fine (if a little pointless).

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.