在asp.net中ilist和list之间的区别 [英] Difference between ilist and list in asp.net

查看:97
本文介绍了在asp.net中ilist和list之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以清楚地了解ilist和list.Please给我一些例子,以便我可以清楚地理解。



谢谢。

解决方案

嗨!



只想添加一些东西......



想法是针对界面编程,而不是实施。因此IList是用于实现List的接口(契约)。例如,如果要编写一个返回并接受对象集合的方法,则应编写返回和接受接口的方法。您想要使用的界面取决于您希望在该方法中使用哪种界面。

例如,假设您只想迭代集合,那么您应该使用IEnumerable的最小接口类型,而不是具体实现(List,Collection,ArrayList,StringCollection等等)。 ..)。返回并接受已定义的界面,您只能保证或承诺您的组件,方法或某些东西会以某种方式运行。查看列表类中定义的 AddRange 方法:

  public   void  AddRange(
IEnumerable< t> collection
< / t >



使用此方法,您可以将多个项添加到给定的列表实例中。所以仔细看看它的定义。它被定义为接受IEnumerable。这种方式的作者为您提供了解决方案,您可以将任何集合类型添加到给定列表中,因为每个集合都实现了IEnumebrable,并且它的最小接口类型支持对集合进行简单迭代。



如果您想了解更多关于如何使用以及使用的接口的信息,请查看书籍 [ ^ ] ...


你好,



所以列表对象允许你创建一个列表,添加它,删除它,更新它,索引它等等,这一切都非常好。只要你想要一个通用的 List ,你就可以使用 List ,你可以在其中指定对象类型,就是这样。



IList 另一方面是接口。 (有关接口的更多信息,请参阅 MSDN接口 [ ^ ])。基本上,如果你想创建自己的 List 类型,比如名为 SimpleList 的列表类,那么你可以使用接口为您提供新类的基本方法和结构。 IList 适用于您想要创建自己的特殊子类,该子类实现 List



如需进一步阅读,请参阅:

MSDN列表 [ ^ ]

MSDN IList [ ^ ] - 具体查看示例代码和看看SimpleList类。



希望这会有所帮助,

Ed


区别在于IList是一个接口,无法实例化。 List是一个类,可以实例化。

即:你不能说

 IList< string> list =  new  IList< string>(); 

但你可以说

 List< string> list =  new 列表< string> 





表示你​​自己的班级可以从两者派生,但使用IList接口,您可以从普通类(例如MyClass)和IList派生:

您可以说:

< pre lang =c#> public class MyClassList:MyListsClass,IList< MyClass>

但你不能说

  public   class  MyClassList:MyListsClass,列表< MyClass> 

因为这需要C#派生自多个类,但它不能。


Can anybody clearly understand me about ilist and list.Please give me some example so that i can cleary understand.

thank you .

解决方案

Hi!

Just want to add a few things...

The idea is to program against the interface, not the implementation. So IList is interface (contract) which is used to implement List. For example if you want to write a method that return and accept "collection" of objects, you should write your method to return and accept interfaces. What interface you want to use depends on want you want to do in that method.
For example, lets say if you just want to iterate over collection then you should use the "smallest" interface type which is IEnumerable, not concrete implementation (List, Collection, ArrayList, StringCollection and so on...). Returning and accepting defined interface you only guaranties or promising that you component, method or something will behave on certain way. Look this AddRange method that is defined on List class:

public void AddRange (
    IEnumerable<t> collection
)</t>


Using this method you can add several items into given list instance. So look its definition closely. It is defined to accept IEnumerable. this way author of this method provided you solution that you can add any collection type into given list, because every collection have IEnumebrable implemented and its the "smallest" interface type which supports a simple iteration over a collection.

If you want to read more about how and for what are interfaces used, take a look at this book[^]...


Hi there,

So a List object allows you to create a list, add things to it, remove it, update it, index into it etc etc and that's all very good. List is used whenever you just want a generic List where you specify object type in it and that's it.

IList on the other hand is an Interface. (For more on interfaces see MSDN Interfaces[^]). Basically, if you want to create your own type of List, say a list class called SimpleList, then you can use the Interface to give you basic methods and structure to your new class. IList is for when you want to create your own, special sub-class that implements List.

For further reading see:
MSDN List[^]
MSDN IList[^] - specifically look at the example code and see the SimpleList class.

Hope this helps,
Ed


The difference is that IList is an Interface and cannot be instantiated. List is a class and can be instantiated.
Ie: you can't say

IList<string> list = new IList<string>();

But you can say

List<string> list = new List<string>



The means that your own class can derive from both, but with an IList interface you can derive from a "normal" class ("MyClass" for example) and IList:
You can say:

public class MyClassList: MyListsClass, IList<MyClass>

But you can't say

public class MyClassList: MyListsClass, List<MyClass>

Because that would require C# deriving from multiple classes, which it can't.


这篇关于在asp.net中ilist和list之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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