IList<ProductInfo> productsBySearch = new List<ProductInfo>();
上面这句话是什么意思?是把类的实例转换为借口类型吗?

解决方案 »

  1.   

    IList<ProductInfo>  productsBySearch  =  new  List<ProductInfo>();  
    上面这句话是什么意思?是把类的实例转换为接口类型吗?
      

  2.   

    list  productsBySearch  里面必须放ProductInfo类的对象泛型
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070210http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  3.   

    注意前面是IList而后面是List,是不一样的?IList是个借口,而List是一个类!!
      

  4.   

    正因为IList是个借口,不能实例化,所以后面必须是一个继承了IList的类。这样程序只用考虑借口定义的方法,不用管特定类的使用IList<ProductInfo>  productsBySearch  =  new  List<ProductInfo>();  
    IList<ProductInfo>  productsBySearch  =  new  List2<ProductInfo>();  //假设另一个实现ilist的类这2个productsBySearch  使用起来就没有差别啦
      

  5.   

    IList<string> pp = new List<string>();
    ICollection<string> aa =(ICollection<string>) pp;这里边pp、aa全都指向同一个对象。我不知道你说的“转换”是个什么意思。但是要注意并没有任何对象转换,强制类型转换是指对对象的索引变量的类型转换或者说是说明,而不是对象本身。“new List<string>();”创建了一个List<string>类型的对象,然后用变量pp指向它(索引它),并且pp的目标对象类型声明为IList<string>。之后又用另一个变量aa指向它,并且aa的目标对象的类型记录为ICollection<string>。自始至终,目标对象没有改变,改变只是创建的新变量中的某个“目标类型”属性。
      

  6.   

    并且对于第一行,编译器发现赋值对象是“安全的”,只是缩小定义范围,因此不会出现异常。而对于第二行,编译器并不跟踪分析pp指向的对象的实际类型,它只知道 ICollection<string> 类型的对象赋值给 ICollection<string> 类型的变量是不安全的,所以你必须明确写上强制类型转换,这时候如果真的pp代表的对象的类型不能被 ICollection<string> 所标记,只有运行时才能出现异常让你的程序垮掉,运行前在编译时就检查不出来了。
      

  7.   

    它只知道 ICollection<string> 类型的对象  -->  它只知道 IList<string> 类型的对象
      

  8.   

    我知道,由于类List<ProductInfo>实现了IList<ProductInfo>接口,那请问下面两种写法有何区别?
    1、IList<ProductInfo> productsBySearch = new  List<ProductInfo>();
    2、IList<ProductInfo> productsBySearch = IList<ProductInfo>(new List<ProductInfo>());
    还有就是如果一个类实现了某个接口,那这个类是否要实现这个接口的所有方法呢?
      

  9.   

    我知道,由于类List<ProductInfo>实现了IList<ProductInfo>接口,那请问下面两种写法有何区别?
    1、IList<ProductInfo> productsBySearch = new  List<ProductInfo>();
    2、IList<ProductInfo> productsBySearch = IList<ProductInfo>(new List<ProductInfo>());
    还有就是如果一个类实现了某个接口,那这个类是否要实现这个接口的所有方法呢?