C# foreach循环问题

C# foreach循环问题,c#,foreach,C#,Foreach,是否有其他更短/更有效的方法来检查它是否是我列表框中的最后一项?这里的主要目标基本上是将所选项目添加到标签中,并在每个项目后面添加逗号,但最后一个项目除外。有什么建议吗 int sc = 0; List<string> interestitems = new List<string>(); foreach (ListItem siitem in ListBox1.Items) { i

是否有其他更短/更有效的方法来检查它是否是我列表框中的最后一项?这里的主要目标基本上是将所选项目添加到标签中,并在每个项目后面添加逗号,但最后一个项目除外。有什么建议吗

        int sc = 0;
        List<string> interestitems = new List<string>();

        foreach (ListItem siitem in ListBox1.Items)
        {
            if (siitem.Selected == true)
            {
               interestitems.Add(siitem.Value.ToString());
            }
        }

        foreach (string inteitem in interestitems)
        {
            Label1.Text += inteitem;
            sc++;
            if (sc < interestitems.Count)
            {
                Label1.Text += ",";
            }
        }
intsc=0;
List interestitems=新列表();
foreach(ListBox1.Items中的ListItem siitem)
{
if(siitem.Selected==true)
{
Add(siitem.Value.ToString());
}
}
foreach(利息中的字符串inteitem)
{
标签1.Text+=inteitem;
sc++;
如果(sc<利息数)
{
标签1.Text+=“,”;
}
}

使用以下命令代替第二个循环:

Label1.Text = string.Join("," , interestitems);

p.S.

如果您使用的是.net 3.5,则需要将字符串数组传递给
string.Join()
,然后:

Label1.Text = string.Join("," , interestitems.ToArray());

编辑:

如果要完全避免循环,请执行以下操作:

var selItems = ListBox1.Items.Cast<ListItem>()
                       .Where(item => item.Selected)
                       .Select(item => item.ToString());

Label1.Text = string.Join("," , selItems);
var selItems=ListBox1.Items.Cast()
.Where(项=>item.Selected)
.Select(item=>item.ToString());
Label1.Text=string.Join(“,”,selItems);

使用以下命令代替第二个循环:

Label1.Text = string.Join("," , interestitems);

p.S.

如果您使用的是.net 3.5,则需要将字符串数组传递给
string.Join()
,然后:

Label1.Text = string.Join("," , interestitems.ToArray());

编辑:

如果要完全避免循环,请执行以下操作:

var selItems = ListBox1.Items.Cast<ListItem>()
                       .Where(item => item.Selected)
                       .Select(item => item.ToString());

Label1.Text = string.Join("," , selItems);
var selItems=ListBox1.Items.Cast()
.Where(项=>item.Selected)
.Select(item=>item.ToString());
Label1.Text=string.Join(“,”,selItems);
LINQ怎么样:

Label1.Text = string.Join(
    ",", 
    ListBox1.Items
            .OfType<ListItem>()
            .Where(item => item.Selected)
            .Select(x => x.Value.ToString())
            .ToArray()
);
Label1.Text=string.Join(
",", 
列表框1.项目
第()类
.Where(项=>item.Selected)
.Select(x=>x.Value.ToString())
.ToArray()
);
LINQ怎么样:

Label1.Text = string.Join(
    ",", 
    ListBox1.Items
            .OfType<ListItem>()
            .Where(item => item.Selected)
            .Select(x => x.Value.ToString())
            .ToArray()
);
Label1.Text=string.Join(
",", 
列表框1.项目
第()类
.Where(项=>item.Selected)
.Select(x=>x.Value.ToString())
.ToArray()
);

您可以用一些LINQ替换所有代码:

Label1.Text = String.Join(", ",
              ListBox1.Items.Cast<ListItem>()
                            .Where(i => i.Selected)
                            .Select(i => i.Value.ToString())
              );
Label1.Text=String.Join(“,”,
ListBox1.Items.Cast()
.其中(i=>i.Selected)
.Select(i=>i.Value.ToString())
);

在.Net 3.5中,您需要添加
.ToArray()

您可以用一些LINQ替换所有代码:

Label1.Text = String.Join(", ",
              ListBox1.Items.Cast<ListItem>()
                            .Where(i => i.Selected)
                            .Select(i => i.Value.ToString())
              );
Label1.Text=String.Join(“,”,
ListBox1.Items.Cast()
.其中(i=>i.Selected)
.Select(i=>i.Value.ToString())
);

在.Net 3.5中,您需要添加
.ToArray()

我相信您可以做到这一点:

interestitems.IndexOf(inteitem);
虽然它对其他类型的物品有用,但可能会给你一个想法。我还没有检查它是否适用于字符串


您只需删除最后一个,并通过索引检查它是否是最后一个有兴趣的。Count

我相信您可以做到这一点:

interestitems.IndexOf(inteitem);
虽然它对其他类型的物品有用,但可能会给你一个想法。我还没有检查它是否适用于字符串


您只需删除最后一个,并使用索引检查它是否是最后一个有兴趣的字符串。Count

为什么不在第一个循环中迭代时构建字符串呢

var builder = new StringBuilder();
var first = true;
foreach (var item in ListBox1.Items) {
  if (item.Selected) {
    if (!first) {
      builder.Append(", ");
    }
    first = false;
    builder.Append(item.Value.ToString());
  }
}

Label1.Text = builder.ToString();

为什么不在第一个循环中迭代时构建字符串呢

var builder = new StringBuilder();
var first = true;
foreach (var item in ListBox1.Items) {
  if (item.Selected) {
    if (!first) {
      builder.Append(", ");
    }
    first = false;
    builder.Append(item.Value.ToString());
  }
}

Label1.Text = builder.ToString();

+这是个好主意。通常我只是使用一个
StringBuilder
,然后删除“,”的最后一个实例。也许你需要
string.Join(“,”,interestitems.ToArray())
我希望我能将此标记为答案。那个经常重复的逗号循环让我发疯。@ja72:不在.NET4中,不过添加了一个旁注;)@里奇-我同意。我认为大多数人在看到
string.Join()
时会将其与
string.Concat()
混淆,不会进一步研究它的作用。感谢微软从一开始就将它包含在.NET中,因为我现在已经使用它1000次了。+1这是个好主意。通常我只是使用一个
StringBuilder
,然后删除“,”的最后一个实例。也许你需要
string.Join(“,”,interestitems.ToArray())
我希望我能将此标记为答案。那个经常重复的逗号循环让我发疯。@ja72:不在.NET4中,不过添加了一个旁注;)@里奇-我同意。我认为大多数人在看到
string.Join()
时会将其与
string.Concat()
混淆,不会进一步研究它的作用。感谢Microsoft从一开始就将其包含在.NET中,因为我现在已经使用了1000次。请不要使用像
Label1
ListBox1
这样的名称。请不要使用像
Label1
ListBox1