C# ILIST NEDIR SEçENEKLER

C# IList Nedir Seçenekler

C# IList Nedir Seçenekler

Blog Article

Else use List. You kişi't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List derece an IList, else you are breaking Liskov substitution principle: "if S is a subtype of T, then objects of type T may be replaced with objects of type S"

IList is an interface so you gönül inherit another class and still implement IList while inheriting List prevents you to do so.

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 nice interface-related soundbites are, you're deluding yourself.

David MillsDavid Mills 2,41511 gold badge2323 silver badges2525 bronze badges 6 2 The right approach really would have been to offer an ISortableList interface (with methods to sort a portion of the list using some particular comparer), have List implement it, and have a static method which could sort any IList by checking whether it implemented ISortableList and, if not, copying it to an array, sorting that, clearing the IList, and re-adding the items.

In this specific case since you're essentially talking about a language construct, not a custom one it generally won't matter, but say for example that you found List didn't support something you needed.

I would turn the question around a bit, instead of justifying why you should use the interface over the concrete implementation, try to justify why you would use the concrete implementation rather than the interface. If you sevimli't justify it, use the interface.

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

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 hamiş 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 not have to change; it knows what the IList Interface contract adheres to.

; being aware of the definition of the interface ie. all abstract methods that are there to be implemented by any class inheriting the interface. so if some one makes a huge class of C# IList Nerelerde Kullanılıyor 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.

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

IList is not a class; it's an interface that classes güç implement. C# IList Nerelerde Kullanılıyor The interface itself is just a contract between the consumer of the class and the class itself. This line of code will work:

The most important case for using interfaces over implementations is in the parameters to your API. If your API takes a List parameter, then anyone who uses it C# IList Neden Kullanmalıyız katışıksız to use 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 bey required.

IEnumerable allows you to iterate through a collection. C# IList Kullanımı ICollection builds on this and also allows for adding and removing items. IList also allows for accessing and modifying them at a specific C# IList Kullanımı index. By exposing the one that you expect your consumer to work with, you are free to change your implementation. List happens to implement all three of those interfaces. If you expose your property birli a List or even an IList when all you want your consumer to have is the ability to iterate through the collection. Then they could come to depend on the fact that they yaşama modify the list.

Report this page