C# ILIST NERELERDE KULLANıLıYOR - GENEL BAKış

C# IList Nerelerde Kullanılıyor - Genel Bakış

C# IList Nerelerde Kullanılıyor - Genel Bakış

Blog Article

I know there saf been a lot of posts on this but it still confuses me why should you pass in an interface like IList and return an interface like IList back instead of the concrete list.

Then the person calling the method is free to call it with any data type that is enumerable. This allows your code to be used in unexpected, but perfectly valid ways.

Interface’ler için elan bir tomar malumat peyda etmek isterseniz, adidaki kaynaklara çekmece atabilirsiniz:

Bearing this in mind, it makes most sense to pass types with the least number of external dependencies possible and to return the same. However, this could be different depending on the visibility of your methods and their signatures.

This will allow me to do generic processing on almost any array in the .Kupkuru framework, unless it uses IEnumerable and hamiş IList, which happens sometimes.

IList on the other hand is an Interface. Basically, if you want to create your own custom List, say a list class called BookList, then you hayat 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.

use LINQ to perform the conversion of your existing List when you return it - but it would be better to just create a more appropriate type to start C# IList Nasıl Kullanılır with, bey shown above.

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

In most cases, if you are using a List and C# IList Nasıl Kullanılır you think you could use a narrower interface instead - why not IEnumerable? This is often a better fit if you don't need to add items. If you need to add to the collection, use the concrete type, List.

List implements those nene methods (derece including extension methods), on ferde of that it katışıksız about 41 public methods, which weighs in C# IList Neden Kullanmalıyız your consideration of which one to use in your application.

IList is derece a class; it's an interface that classes gönül implement. The interface itself is just a contract between the consumer of the class and the class itself. This line of code will work:

Brad LeachBrad Leach 17k1818 gold badges7373 silver badges8888 bronze badges 1 3 It will create a new enumerable, which may derece be desirable in some scenarios. You cannot sort an IList in-place through C# IList Nasıl Kullanılır the interface except by using ArrayList.Adapter method in my knowledge.

So typically, your methods should accept and return interfaces for collections. This leaves your own implementation and your callers room to decide on the actual implementation as required.

additional advantage is that your code is safe from any changes to concrete class as you are subscribing C# IList Nasıl Kullanılır to only few of the methods of concrete class and those are the ones that are going to be there birli long birli the concrete class inherits from the interface you are using.

Report this page