C# 从c中foreach循环内的else-if循环中断#

C# 从c中foreach循环内的else-if循环中断#,c#,C#,我有一个foreach循环,里面有if-else循环,在每个循环中我检查文本文件的行,首先我检查第一行是“ctf”,如果不是从所有循环中退出,否则它是“ctf”,然后在foreach循环中取下一行,转到else部分,但我的另一部分是检查第一行,谁能说出实际问题是什么 bool first = true;int i=0; lines = streamReader.ReadToEnd().Split("\r\n".ToCharArray(), String

我有一个foreach循环,里面有if-else循环,在每个循环中我检查文本文件的行,首先我检查第一行是“ctf”,如果不是从所有循环中退出,否则它是“ctf”,然后在foreach循环中取下一行,转到else部分,但我的另一部分是检查第一行,谁能说出实际问题是什么

     bool first = true;int i=0;
       lines = streamReader.ReadToEnd().Split("\r\n".ToCharArray(),           StringSplitOptions.RemoveEmptyEntries);
      foreach (string  line in lines)
             {
               if (first)
                {
                    if (line != "CTF") { break; }    // i think the problem is here.
                    first = false;

                }
                else
                {

                    tabs = line.Split('\t');
                    ID = int.Parse(tabs[0]);
                    X = int.Parse(tabs[1]);
                    Y = int.Parse(tabs[2]);
                    H = int.Parse(tabs[3]);
                    W = int.Parse(tabs[4]);
                    Text = tabs[5];
                    ItemTypes types = (ItemTypes)int.Parse(tabs[6]);

                        Items.Add(new FormItem());
                        Items[i].Id = ID;
                        Items[i].X = X;
                        Items[i].Y = Y;
                        Items[i].Height = H;
                        Items[i].Width = W;
                        Items[i].Text = Text;
                        Items[i].Type = types;
                       i++;


                }

交换“如果”检查主体的顺序

        if (first)
            {
                first = false;
                if (line != "CTF") { break; }
            }

你的问题是,如果第一行不是“CTF”,布尔变量“第一”不会被设置为假。

< P>假设它是一个外壳问题,考虑下面的变化:…< /P>
if (line != "CTF") { break; } 


祝你好运

write first=在else之后为false

foreach (string  line in lines)

             {
               if (first)
                {
                    if (line != "CTF") { break; }    // i think the problem is here.

                }
                else
                {

                    tabs = line.Split('\t');
                    ID = int.Parse(tabs[0]);
                    X = int.Parse(tabs[1]);
                    Y = int.Parse(tabs[2]);
                    H = int.Parse(tabs[3]);
                    W = int.Parse(tabs[4]);
                    Text = tabs[5];
                    ItemTypes types = (ItemTypes)int.Parse(tabs[6]);

                        Items.Add(new FormItem());
                        Items[i].Id = ID;
                        Items[i].X = X;
                        Items[i].Y = Y;
                        Items[i].Height = H;
                        Items[i].Width = W;
                        Items[i].Text = Text;
                        Items[i].Type = types;
                       i++;


                }
    first = false;
}

这个问题已经得到了回答,但一般来说,如果你必须打破一个循环,最好使用Do-While循环而不是Foreach。
foreach (string  line in lines)

             {
               if (first)
                {
                    if (line != "CTF") { break; }    // i think the problem is here.

                }
                else
                {

                    tabs = line.Split('\t');
                    ID = int.Parse(tabs[0]);
                    X = int.Parse(tabs[1]);
                    Y = int.Parse(tabs[2]);
                    H = int.Parse(tabs[3]);
                    W = int.Parse(tabs[4]);
                    Text = tabs[5];
                    ItemTypes types = (ItemTypes)int.Parse(tabs[6]);

                        Items.Add(new FormItem());
                        Items[i].Id = ID;
                        Items[i].X = X;
                        Items[i].Y = Y;
                        Items[i].Height = H;
                        Items[i].Width = W;
                        Items[i].Text = Text;
                        Items[i].Type = types;
                       i++;


                }
    first = false;
}