CS0029 C# 'Cannot implicitly convert type string[] to string'

CS0029 C# 'Cannot implicitly convert type string[] to string'

我正在制作一个应用程序来编辑 .mp3 文件的属性。我不得不说我是编程和 Whosebug 的新手,所以我可能会做一些非常明显的错误。请原谅我!这是我使用的代码:

private void btnApply_Click(object sender, EventArgs e)
{
    var file = TagLib.File.Create(filepath);
    if (!string.IsNullOrWhiteSpace(txtGenre.Text))
    {
        file.Tag.Genres = new string[] {txtGenre.Text};
    }
    if (!string.IsNullOrWhiteSpace(txtArtist.Text))
    {
        file.Tag.Performers = new string[] {txtArtist.Text};
    }
    if (!string.IsNullOrWhiteSpace(txtTitle.Text))
    {
        file.Tag.Title = new string[] {txtTitle.Text};
    }
    file.Tag.Performers = new string[] { txtArtist.Text };
    file.Tag.Title = txtTitle.Text;
    file.Save();

    if (!ReadFile())
    {
        Close();
    }
}

对我来说奇怪的是我只得到这部分的错误:

if (!string.IsNullOrWhiteSpace(txtTitle.Text))
{
    file.Tag.Title = new string[] {txtTitle.Text};
}

红色下划线:

new string[] {txtTitle.Text}

我在这里错过了什么?我一直在寻找很长时间,但似乎找不到任何解决方案。先感谢您!顺便说一下,我也在使用 TagLib。

改变这个:

file.Tag.Title = new string[] {txtTitle.Text};

至:

file.Tag.Title = txtTitle.Text;

Title 类型是 string,不是字符串数组(不是 string[]),但您尝试分配数组 - 因此会出错。其他字段的类型为 string[](字符串数组),这就是为什么只有 Title.

才会出错的原因

此外,您尝试为 Title 赋值 2 次:

if (!string.IsNullOrWhiteSpace(txtTitle.Text))
{
    file.Tag.Title = new string[] {txtTitle.Text};    // first time
}
file.Tag.Performers = new string[] { txtArtist.Text };
file.Tag.Title = txtTitle.Text;                       //second time

您只需分配一次。另外,当你第二次分配时,你分配正确没有错误。

Performers 相同的情况 - 您在 if 语句中第一次赋值,在最后一个 if.

之后第二次赋值

像这样更改代码并尝试

file.Tag.Title = txtTitle.Text;