c# - Foreach 问题

标签 c# foreach

我正在尝试使用 foreach 语句。我希望能够做这样的事情:

foreach (int item in itemcount)
  {
     label{0}.Text = item.ToString()
  }

其中 {0} 是项目中的数字。

这可能吗?

最佳答案

你可以这样做:

foreach (int item in itemcount)
{
    Controls[string.Format("label{0}", item.Id)].Text = item.ToString();
}

此外,如果您需要访问标签类的属性,您需要将左表达式转换为标签类。

关于c# - Foreach 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875526/

相关文章:

c# - 哪个更好?强制转换可为 null 的类型或使用 Value 属性检索值?

C# : Dealing with HttpWebResponse timeout problems

java - 迭代列表时删除元素

vba - 如何使 For-Each 循环向后运行

java - 如何比较foreach循环中的值

c# - 数据包格式不正确,使用 WSDL 文件从 C# 调用 SAP 服务

c# - 不应该使用 using 语句和 IDisposable 的场合

c# - 无法为 LINQ 选择的数据分配新值

c++ - for_each(for_each())?

c# - 根据字符串大小写对列表进行排序