坛友问:
//定义存储每个柱子上可移动标签的泛型集合
List<List<Label>> seleckedCanMoveLb = new List<List<Label>>();
for (int i = 0; i < 3; i++)
{
seleckedCanMoveLb = new List<Label>();
}
for (int i = 0; i < seleckedCanMoveLb.Count; i++)
{
seleckedCanMoveLb
}
for (int i = 0; i < seleckedCanMoveLb.Count; i++)
{
seleckedCanMoveLb
}
都不对,应该如何访问List集合中的元素?
答:
List<System.Windows.Forms.Label> pLabelList = new List<System.Windows.Forms.Label>();
pLabelList.Add(this.label1);
pLabelList.Add(this.label2);
////方法一:
<span style="color:#ff0000;">foreach (System.Windows.Forms.Label pLabel in pLabelList)</span>
{
string sName = pLabel.Name;
}
///方法二:你基础太不扎实了,最基本的语法还是要好好看的
<span style="color:#ff6666;">for (int i = 0; i < pLabelList.Count; i++)</span>
{
System.Windows.Forms.Label pLabel = pLabelList[i];
string sName = pLabel.Name;
}
本文解答了一位坛友关于如何正确访问多级List集合中元素的问题,并提供了两种常见且有效的遍历方法,包括使用foreach循环及for循环进行访问。
345

被折叠的 条评论
为什么被折叠?



