开发者

What is IList used for?

I have already searched for it on MSDN and I read the definition that it is a non generic .... but my problem is开发者_如何学运维 that i am not understanding what is its use and when to use it . So i hope that anyone can help me . Thanks .


In addition to the older code that doesn't know about generics, there are also a lot of cases where you know you have a list of data, but you can't know of what ahead of time. Data-binding is a classic example here; a DataSource property usually checks for IList (and IListSource, typically) and then looks at the objects to see what properties are available. It can't use generics in this case.

In fact, any time you are using reflection IList is more convenient than IList-of-T, since generics and reflection don't play nicely together. It can be done, but it is a pain. Unfortunately since IList-of-T doesn't derive from IList there are cases where this can fail - but it is a good 95% rule.

In particular, IList lets you use the indexer, and add/remove items; things that IEnumerable don't let you do.

Most app-level code (i.e. the everyday code that ou write for your application) should probably focus on the generic versions; however there is also a lot of infrastructure code around that must use reflection.


IList (non-generic version) is a leftover from C# version one, when we didn't have generics.

With C# 2 and .NET Framework 2.0, generics was added to the language and generic implementations of the collection types was added. Therefore, the non-generic versions was no longer needed, but they couldn't be removed from the framework, since it would make porting code from .NET 1.1 to 2.0 a pain.

Today, you almost always use IList<T>, the primary reason for IList to still be around is for reasons of backwards compatibility.


It is an interface that existed before generics were introduced in .NET 2.0.

It is there for backwards compatibility.

Use the generic version.


it is a interface , which is normally implemented by collection classes which is intended to provide list like method but in .net version less than 2.0 i.e. without generic introduction so if you are using framework version 1.0, 1.1 than you would see the usage of this IList

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜