看一教程的ComboBox定制,看不懂下面的代码,谁能帮忙解释下,非常感谢^_^主要目的是为了实现动态填充ComboBox1、先是新建了一个Product类,主要看不懂的地方在代码中有标注:namespace MyComboBox
{
    public class Product
    {
        public Product(string code, string name)
        {
            this._code = code;
            this._name = name;
        }        private string  _code;
        public string  Code
        {
            get { return _code; }
            set { _code = value; }
        }        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }        public override string ToString()
        {
            return _code + " " + _name;
        }
        
        //这个方法看不太懂,怎么一会IList,一会List,这里是泛型方法么?希望能得到您的详解,谢谢!
        public static IList<Product> GetAllProducts()
        {
            IList<Product> all=new List<Product>();
            all.Add(new Product ("01", "浙江"));
            all.Add(new Product ("02","杭州"));
            all.Add(new Product ("03","千岛湖"));
            return all;
        }
    }
}
2、通过点击按钮实现动态填充:private void button1_Click(object sender, EventArgs e)
        {
            IList<Product> all = Product.GetAllProducts();            foreach (Product prod in all)
            {
                this.comboBox1.Items.Add(prod);
            }
        }
3、选中某一项时弹出消息显示选择的Product的Name:        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Product selectedProduct = (Product)this.comboBox1.SelectedItem;
            MessageBox.Show(selectedProduct.Name);
        }
谢谢您看完帖子。

解决方案 »

  1.   

    IList是接口,List是类。
    例如
    Interface IShape
    {
        void Draw();
    }class Circle : IShape
    {
        void Draw()
        {
            //Draw的实现
        }
    }你可以将Circle的实例付给IShape类型的引用
    IShape shape = new Circle();
      

  2.   

    IList是接口,List是类。 
    在public static IList<Product> GetAllProducts()
    中接口作为一个返回值,返回的其实就是list集合
      

  3.   

    而Ilist 和List有点难理解,是因为它结合了generic.
    实际上你的IList<Product>使用的generic class是IList<T>
    它应该类似于下面的样子
    public interface IList<T> : ICollection<T>, 
    IEnumerable<T>, IEnumerable
    {
         void Add(T item);   //因为是interface因此只有声明,没有实现。
          ...其它接口
    }而List<Product> 使用的是List<T>
    它看起来像这个样子:
    public class List<T> : IList<T>, ICollection<T>, 
    IEnumerable<T>, IList, ICollection, IEnumerable
    {
        void Add(T item)
        {
           //实现
        }
    }需要注意的是,你用一种类型实例化List<T>,只能付给使用这种类型的引用的接口IList<T>
    例如
    IList<int> a = new List<int>()   //corrent
    IList<int> b = new List<double>()  //compile error.good luck
      

  4.   

    实际上你的IList <Product>使用的generic class是IList <T> 
    它应该类似于下面的样子 
    public interface IList <T> : ICollection <T>, 
    IEnumerable <T>, IEnumerable 

        void Add(T item);  //因为是interface因此只有声明,没有实现。 
          ...其它接口 
    } 而List <Product> 使用的是List <T> 
    它看起来像这个样子: 
    public class List <T> : IList <T>, ICollection <T>, 
    IEnumerable <T>, IList, ICollection, IEnumerable 

        void Add(T item) 
        { 
          //实现 
        } 
    } 需要注意的是,你用一种类型实例化List <T>,只能付给使用这种类型的引用的接口IList <T> 
    例如 
    IList <int> a = new List <int>()  //corrent 
    IList <int> b = new List <double>()  //compile error. 
      

  5.   

    IList是有索引的列表,是接口
    List是泛型。
      

  6.   

    IList  是泛型接口 .
    List是泛型类。 
      

  7.   


    public static IList<Product> GetAllProducts()
            {
                  
                IList<Product> all=new List<Product>();
                all.Add(new Product ("01", "浙江"));
                all.Add(new Product ("02","杭州"));
                all.Add(new Product ("03","千岛湖"));
                return all;
            }
    应该是一个泛型实体吧  
    all.Add(new Product ("01", "浙江")); Add方法就是往实例里加东西