错误:无法将类型'string'隐式转换为'string []'?

时间:2018-04-29 14:05:49

标签: c# arrays string

我试图使用.txt文件创建一个数组,其中有单词。每个数组只有一个单词。但在那里,fajlbe.ReadLine();被固定为错误的代码。为什么?

struct Sor
{
    public string árucikk;
}
static Sor[] TSor = new Sor[1000];

static void Main(string[] args)
{
    StreamReader fajlbe = new StreamReader("penztar.txt");
    string[] tomb = new String[1];

    int n = 0;

    while(!fajlbe.EndOfStream)
    {
        tomb = fajlbe.ReadLine();   //here I've got an error
        TSor[n].árucikk = tomb[0];
        n++;
    }
    Console.WriteLine(TSor[1]);

    fajlbe.Close();

1 个答案:

答案 0 :(得分:0)

tomb = fajlbe.ReadLine();

ReadLine方法返回一个字符串,而变量tomb被声明为字符串数组(string[] tomb = new String[1];)。您不能将字符串分配给字符串数组。

相反,如果您打算将字符串分配给数组的第一个元素,可以通过设置数组的第一个元素来实现:

tomb[0] = fajlbe.ReadLine();

在这里看起来你不需要一个阵列。如果是这样,您只需更改变量tomb的声明即可将其声明为字符串:

string tomb = string.Empty;

当然,你需要改变,

TSor[n].árucikk = tomb[0];

要,

TSor[n].árucikk = tomb;
相关问题