C# IList Kullanımı - Genel Bakış

Wiki Article

Else use List. You can't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List not an IList, else you are breaking Liskov substitution principle: "if S is a subtype of T, then objects of type T may be replaced with objects of type S"

Same principle kakım before, reversed. Offer the bare asgari that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable.

So I came across an interesting sorun today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it.

Your functions above only care about being able to iterate over a list. Ideally they shouldn't need to know who implements that list or how they implement it.

Typically, a good approach is to use IList in your public facing API (when appropriate, and list semantics are needed), and then List internally to implement the API. This allows you to change to a different implementation of IList without breaking code that uses your class.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

List implements IList, and so emanet be assigned to the variable. There are also other types that also implement C# IList Neden Kullanmalıyız IList.

Now I am returning IList for the simple fact that I will then add this to my domain manken what saf a property like this:

Bir dahaki sefere versiyon yaptığımda kullanılmak üzere aşamaı, elektronik posta adresimi ve web şehir adresimi bu tarayıcıya kaydet.

tranmqtranmq 15.5k33 gold badges3232 silver badges2727 bronze badges 6 But in this case I dirilik't bind my collection to DataGridView rather I have to expose the _list member in MyCollection.

I know that IList is the interface and List is the concrete type but I still don't know when to use each one. What I'm doing now is if I don't need the Sort or FindAll methods I use the interface. Am I right? Is there a better way to decide when to use the interface or the concrete type?

Benefit of using an Interface is that you get to implement your functionality C# IList Neden Kullanmalıyız or better yet, the only functionality you require. So, if iteration/enumeration is required only, then there is no need for the Sort, Add methods.

If you're just enumerating over the values, you should be using C# IList Nasıl Kullanılır IEnumerable. Every type of datatype that yaşama hold more than one value implements IEnumerable (or should) and makes your method hugely flexible.

The other general reason C# IList Kullanımı for using interfaces is to expose the asgari amount of knowledge necessary to the user of an object. Consider the (contrived) case where I have a C# IList Nasıl Kullanılır veri object that implements IList.

Report this wiki page