C# ILIST NASıL KULLANıLıR NO FURTHER MYSTERY

C# IList Nasıl Kullanılır No Further Mystery

C# IList Nasıl Kullanılır No Further Mystery

Blog Article

Note that, if your API is only going to be used in foreach loops, etc, then you might want to consider just exposing IEnumerable instead.

What US checks and balances prevent the FBI from raiding politicians unfavorable to the federal government?

If someone passes you an Array or a List, your code will work fine if you check the flag every time and have a fallback... But really; who does that?

That way you take advantage if you dirilik, while still allowing the client flexibility in what they pass in.

Basically, I need to see some actual code examples of how using IList would have solved some problem over just taking List into everything.

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 dirilik be sure that your list is derece 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.

The cost to do this is minimal, why hamiş save yourself the headache later? It's what the interface principle is all about.

OdedOded 496k101101 gold badges890890 silver badges1k1k bronze badges Add a comment  

For example, let's say you have a Person class and a Group class. A Group instance başmaklık many people, so a List here would make sense. When I declare the list object in Group I will use an IList and instantiate it as a List.

Then later if you decide C# IList Neden Kullanmalıyız to convert the actual veri store from a List to a Dictionary and expose the dictionary keys bey the actual value for the property (I have had to do exactly this before). Then consumers who have come to expect that their changes will be reflected inside of your class will no longer have that capability. That's a big mesele! If you expose the List birli an IEnumerable you emanet comfortably predict that your collection is derece being modified externally. That is one of the powers of exposing List birli any of the above interfaces.

I know that IList is the interface and List is the concrete C# IList Kullanımı 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?

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

This will help C# IList Nedir if you decide to change the implementation of your class later to use a different concrete class. In that case the users of your library won't need to update their code since the C# IList Nasıl Kullanılır interface doesn't change.

This works, because only the outer list is created in the first place. You birey then insert individual items that are compatible with IList:

Report this page