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 bey before, reversed. Offer the bare minimal that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable.

Interface’ler hakkında elan bir tomar bilim yapmak isterseniz, kötüdaki kaynaklara nazar atabilirsiniz:

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.

IList is a interface and doesn't have any implementation, so the performance of IList depending the class it implements

This level of abstraction goes the other direction when it belongs to method parameters. When you pass your list to a method that accepts IEnumerable you can be sure that your list is hamiş going to be modified. When you are the person implementing the method and you say you accept an IEnumerable because all you need to do is iterate through that list.

You are most often better of using the most general usable type, in this case the IList or even better the IEnumerable interface, so that you hayat switch the implementation conveniently at a C# IList Nerelerde Kullanılıyor later time.

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:

This example also tells you that there may be situations when you need to specify the implementation, hamiş the interface, in the argument list: In this C# IList Nasıl Kullanılır example, whenever you require a particular access performance characteristic.

You would because defining an IList or an ICollection would open up for other implementations of your interfaces.

C# IList Kullanımı By asking for more than you need, you (1) make the caller do unnecessary work to satisfy your unnecessary demands, and (2) communicate falsehoods to the reader. Ask only for what you're going to use. That way if the caller saf a sequence, they C# IList Nedir don't need to call ToList on it to satisfy your demand.

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

And, if you used a generic implementation, you would only be able to use a method that works for any object only with objects of a specific type.

I read a lot of posts saying how this makes it easier to change the implementation later on, but I just don't fully see how that works.

Report this wiki page