Bir İnceleme C# IList Neden Kullanmalıyız
Bir İnceleme C# IList Neden Kullanmalıyız
Blog Article
driisdriis 163k4545 gold badges268268 silver badges343343 bronze badges 3 Sorry, but even now there are plenty of uses for library code to use IList (non-generic). Anyone who says otherwise hasn't suffered enough reflection / veri-binding / etc ;)
Bu şekilde listelerin birbirini point etmesi ve rabıtlı listenin elemanlarına porte verilmesi katkısızlanmaktadır.
lomaxxlomaxx 115k5858 gold badges146146 silver badges180180 bronze badges 1 Why would you return an IList in the first place? From a WCF service?
Then click on the bulb symbol or place the cursor on the IList and press Strg + "." You will become several implementations offered, like:
Basically, I need to see some actual code examples of how using IList would have solved some problem over just taking List into everything.
You güç have an instance of an interface, but you need to initialize it with a class that implements that interface such kakım:
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 with, kakım shown above.
Matthew WatsonMatthew Watson 108k1111 gold badges170170 silver badges290290 bronze badges 2 2 This is trivially true for every interface. If you want to follow through with your argument, than you could argue to never use any interface at all, because some implementation of it might C# IList Nedir throw.
; being aware of the definition of the interface ie. all abstract methods that are there to be implemented C# IList Nerelerde Kullanılıyor by any class inheriting the interface. so if some one makes a huge class of his own with several methods besides the ones he inherited from the interface for some addition functionality, and those are of no use to you, its better to use a reference to a subclass (in this case the interface) and assign the concrete class object to it.
Taking LinkedList vs taking List vs IList all communicate something of the performance guarantees required by the code being called.
Of course that only need apply to methods that are externally visible (i.e. public methods). I personally use interfaces even in internal code, but birli you are able to change all the code yourself if you make breaking changes it's not strictly necessary.
Collaborate with us on GitHub The source for this content güç be found on GitHub, where you sevimli also create and review issues and pull requests. For more information, see our contributor guide.
So typically, your methods C# IList Nedir should accept and return interfaces for collections. This leaves your own implementation and your callers room to decide on the actual implementation bey required.
For instance, if you return an IEnumerable, then you are limiting them to iterating -- they emanet't add or remove items from your object, they gönül only act against the objects. If you need to expose a collection outside of a class, but C# IList Nerelerde Kullanılıyor don't want to let the caller change the collection, this is one way of doing it. On the other hand, if you are returning an empty collection that C# IList Nedir you expect/want them to populate, then an IEnumerable is unsuitable.