ICollection< SomeModel>之间的差异vs List< SomeModel> [英] Difference between ICollection<SomeModel> vs List<SomeModel>

查看:115
本文介绍了ICollection< SomeModel>之间的差异vs List< SomeModel>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型让我们说客户和产品,这是客户和产品之间的一对多,即一个客户可以有多个产品

现在有什么区别

  public   class 客户
{
public int ID { get ; set ; }
public int 名称{ get ; set ; }
public ICollection< Products>产品{获取; set ;}

}

//
public class 客户
{
public int ID {获得; set ; }
public int 名称{ get ; set ; }
public 列表<产品>产品{获取; set ;}

}
// 哪个更适合dataModeling

解决方案

ICollection< T>是一个接口,List< T>是一个类。

这意味着第一个定义可以采用任何实现ICollection接口的类(它支持添加,删除等方法:http://msdn.microsoft.com/en-us/library/92t2ye13(v=vs.110).aspx [ ^ ])List实现ICollection,但扩展它。



第一个定义可以采用任何实现ICollection的类实例< T> - 这包括List< T>以及从它们派生的任何类 - 第二个只能接受List< T>从中得到的类。



第一个比第二个更灵活:它可以采用更广泛的类 - 遗憾的是不包括LINQ值,总是实现IEnumerable< T>但不是ICollection< T>它来自IEnumerable< T>。


ICollection是 INTERFACE ,其中List是 CLASS IMPLEMENTS 那个特定的 INTERFACE

所以第二个选项绑定到类型'List',其中第一个选项可以用于实现ICollection接口的任何类型。 。


请注意,您的代码无法编译(您需要为 ICollection 列表属性)。

在第一种情况下,例如

  public   class 客户
{
public int Id { get ; set ; }
public int 名称{ get ; set ; }
public ICollection< Product> ProdColl {获取; 设置;}

}





你可以使用任何实现 ICollection< T> 接口的类来设置(然后获取) ProdColl 属性,例如

 LinkedList< ProductList> =  new  LinkedList< Porduct>(); 
// 将产品添加到ProductList
Customer Cust = new Cuustomer();
Cust.ProdColl = ProductList;





虽然在第二种情况下,你必须使用 List< Product>


I Have a model Lets say Customer and Products and this is one to many between Customer and Products ie one customer can have multiple products
Now what is difference between

public class Customer
{
public int Id { get; set; }
public int Name{ get; set; }
public ICollection<Products> Product{ get ; set ;}

}

// and 
public class Customer
{
public int Id { get; set; }
public int Name{ get; set; }
public List<Products> Product{ get ; set ;}

}
//And Which is better for dataModeling

解决方案

ICollection<T> is an interface, List<T> is a class.
Which means that the first definition can take any class which implements the ICollection interface (which supports methods such as Add, Remove, and so forth: http://msdn.microsoft.com/en-us/library/92t2ye13(v=vs.110).aspx[^]) where List implements ICollection, but extends it.

The first definition can take any class instance that implements ICollection<T> - this includes List<T> and any class derived from them - the second can only accept List<T> and classes derived from that.

The first is just a little more flexible than the second: it can take a wider range of classes - unfortunately not including LINQ values, which always implement IEnumerable<T> but not ICollection<T> which is derived from IEnumerable<T>.


ICollection is an INTERFACE, where List is a CLASS that IMPLEMENTS that specific INTERFACE!
So the second option is bonded to the type 'List' where the first option can work with any type that implements the ICollection interface...


Please note, your code wouldn't compile (you need to give a name to your ICollection or List property).
In the first case, e.g.

public class Customer
{
public int Id { get; set; }
public int Name{ get; set; }
public ICollection<Product> ProdColl{ get ; set ;}

}



You may use any of the classes implementing the ICollection<T> interface to set (and then get) the ProdColl property, e.g.

LinkedList<ProductList>  = new LinkedList<Porduct>();
// add products to ProductList
Customer Cust = new Cuustomer();
Cust.ProdColl = ProductList;



While, in the second case, you have to use exctly a List<Product>.


这篇关于ICollection&lt; SomeModel&gt;之间的差异vs List&lt; SomeModel&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆