本帖最后由 sunbinjin 于 2014-11-18 23:26:06 编辑

解决方案 »

  1.   

    UIList[] m_pList = 
    {
       new UIList  { cmd = xxx, desc = xxx },
        ...
    }
    注意要定义成public的。
      

  2.   

    1.首先,你应该好好看看书,看看老外的书。
    你的问题是,array数组类型,在声明及定义时,必须指定个数如1000,如下:
                UIList[] myArray = new UIList[1000];
                MessageBox.Show("Creat array done!");
    当然你可以只声明但不初始化。
    2.其次,想简洁高效,就用泛型,可随意添加,通过方法Add()或AddRange();
    而且,你这什么代码,结构体成员全都是默认私有的,你又没属性又没方法,又没构造函数,你搞个毛线。
    所有数组成员必须初始化再使用。
        public class UIList
        {
            public CMD cmd;
            public string desc;
            public Control checkBox;
            public Control label;        public UIList()
            {
                cmd = CMD.white;
                desc = "abcd";
            }
          }
            private void button1_Click(object sender, EventArgs e)
            {
                List<UIList> myList = new List<UIList>();
                myList.Add(new UIList());
                UIList[] myArray = new UIList[3]
                                    {
                                        new UIList(),new UIList(),new UIList()
                                    };
                myList.AddRange(myArray);
                if (myList[0].desc=="abcd"&&myList[0].cmd==CMD.white)
                    MessageBox.Show("See Constructor method");
                var linqResult = from n in myList
                                 where n.desc == "abcd"
                                 select n;
                UIList[] resultArray = linqResult.ToArray();
                MessageBox.Show("Result count is "+resultArray.Length.ToString());
            }
      

  3.   

    最简单的写法应该是加构造函数,然后你就可以new 1000次,如果你的1000个是写在代码里,只好 {new xxx, new xxx, ....1000次} 
    如果是从其他数组复制的,可以用linq的Select或new一个Array[1000]挨个生成。
    这种事先知道数据条数的不适合用泛型List
      

  4.   

    给你写个例子    public struct MyUI
        {
            string A;
            string B;
            int C;        public MyUI(string a, string b, int c)
            {
                A = a;
                B = b;
                C = c;
            }
        }    public class MyList : IEnumerable<MyUI>
        {
            private List<MyUI> lst = new List<MyUI>();        public void Add(string a, string b, int c)
            {
                lst.Add(new MyUI(a, b, c));
            }        public IEnumerator<MyUI> GetEnumerator()
            {
                return lst.GetEnumerator();
            }        IEnumerator IEnumerable.GetEnumerator()
            {
                return lst.GetEnumerator();
            }
        }这里,关键是要实现 IEnumerable,同时有一个 Add 方法用于匹配 { ....} 表达式。最终可以这样调用其实例化和Add方法MyList m_pList = new MyList 

        {"asd","234",2},
        {"k2j","298",3}
    };
      

  5.   

    这个可枚举对象的实例化方法,实际上是c#的语法糖。它帮你简写实例化和Add方法,写成类似数组实例化的简单语法形式。
      

  6.   

    如果你实例化成这样类型的可枚举对象var m_pList = new List<MyUI> 

        {"asd","234",2},
        {"asd","234",2}
    };
    编译时由于找不到有3个参数的Add方法,c#编译器会报错。而只要能够找得到3个参数的Add方法,编译就通过了。