关于 List 泛型集合访问方法

坛友问:

        //定义存储每个柱子上可移动标签的泛型集合
        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;
            }


原文地址:https://www.cnblogs.com/dengshiwei/p/4258657.html