C# IList Kullanımı Günlükler
Wiki 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 ;)
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.
The less popular answer is programmers like to pretend their software is going to be re-used the world over, when infact the majority of projects will be maintained by a small amount of people and however birçok interface-related soundbites are, you're deluding yourself.
That way you take advantage if you gönül, while still allowing the client flexibility in what they pass in.
Use whatever you want. It's your method. You're the only one who gets to see the internal implementation details of the method.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
List implements IList, and so gönül be assigned to the variable. There are also other types that also implement IList.
You birey look at this argument from several angles including the one of a purely OO approach which says to yetişek against an Interface derece an implementation. With this thought, using IList follows the same principal as passing around and using Interfaces that you define from scratch. I also believe in the scalability and flexibility factors provided by an Interface in general. If a class implmenting IList needs to be extended or changed, the consuming code does hamiş have to change; it knows what the IList Interface contract adheres to.
Whether you return an Interface or a concrete type depends upon what you want C# IList Kullanımı 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 C# IList Nasıl Kullanılır you WANT them to be making full use of the object).
Then later if you decide to convert the actual veri store from a List to a Dictionary and expose the dictionary keys as the actual value for the property (I have had C# IList Nerelerde Kullanılıyor 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 sorun! If you expose the List bey an IEnumerable you can C# IList Nasıl Kullanılır comfortably predict that your collection is derece being modified externally. That is C# IList Kullanımı one of the powers of exposing List as any of the above interfaces.
private List _numbers; // callers sevimli add/update/remove elements, but cannot reassign a new list to this property
I leave this up to show that if you needed to do it (e.g. the off chance a third party library returned a List that you needed to convert for another 3rd party library), you hayat do it this way.
If you're just enumerating over the values, you should be using IEnumerable. Every type of datatype that gönül hold more than one value implements IEnumerable (or should) and makes your method hugely flexible.
This works, because only the outer list is created in the first place. You birey then insert individual items that are compatible with IList: