The Ultimate Guide To C# IList Nerelerde Kullanılıyor
Wiki 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.
Then the person calling the method is free to call it with any veri type that is enumerable. This allows your code to be used in unexpected, but perfectly valid ways.
Edit: You do need to be quick to get answers in here. As I presented a slightly different syntax to the other answers, I will leave my answer - however, the other answers presented are equally valid.
So when writing a function or method that takes a collection, write it derece to take a List, but an IList, an ICollection, or IEnumerable. The generic interfaces will still work even for heterogenous lists because System.
Use whatever you want. It's your method. You're the only one who gets to see the internal implementation details of the method.
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 yaşama 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.
In C# IList Kullanımı case of using IList, the caller is always guareented things to work, and the implementer is free to change the underlying C# IList Nerelerde Kullanılıyor collection to any alternative concrete implementation of IList
Matthew WatsonMatthew Watson 108k1111 gold badges170170 silver badges290290 bronze badges 2 2 This is trivially true for every interface. If you want C# IList Nasıl Kullanılır to follow through with your argument, than you could argue to never use any interface at all, because some implementation of it might throw.
Whether you return an Interface or C# IList Nasıl Kullanılır a concrete type depends upon what you want to let your callers do with the object you created -- this is an API design decision, and there's no hard and fast rule. You have to weigh their ability to make full use of the object against their ability to easily use a portion of the objects functionality (and of course whether you WANT them to be making full use of the object).
List implements those büyük anne methods (not including extension methods), on tamamen of that it katışıksız about 41 public methods, which weighs in your consideration of which one to use in your application.
Obviously if you are being asked which you use in an interview, you say IList, smile, and both look pleased at yourselves for being so clever. Or for a public facing API, IList. Hopefully you get my point.
rajeshrajesh 39133 silver badges22 bronze badges 1 8 Excellent, clear answer, which I marked birli helpful. However, I would add C# IList Nerelerde Kullanılıyor that for most developers, most of the time, the tiny difference in program size and performance is not worth worrying about: if in doubt, just use a List.
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 birli required.
This works, because only the outer list is created in the first place. You güç then insert individual items that are compatible with IList: