将char []转换为string []的C#编译错误

时间:2013-11-14 09:59:51

标签: c# string

我正在尝试编译一个c3程序,但我继续收到以下错误。

  

错误CS1502:string.Join(string, string[])的最佳重载方法匹配包含一些无效参数。

     

错误CS1503:参数#2无法将char[]表达式转换为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));

3 个答案:

答案 0 :(得分:3)

答案很简单,arrchar[]而不是string[]

试试这个

Console.WriteLine("| {0} |", string.Join(" | ", arr.Select(a => a.ToString())));

答案 1 :(得分:2)

您可以迭代数组的char s(as suggested by others),也可以更改数组的类型

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));
}

答案 2 :(得分:0)

由于您的arr类型为char[],您可以使用String(char[])构造函数创建string对象实例

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