EN KURALLARı OF C# ILIST KULLANıMı

En Kuralları Of C# IList Kullanımı

En Kuralları Of C# IList Kullanımı

Blog Article

I know there has been a lot of posts on this but it still confuses me why should you pass in an interface like IList and return an interface like IList back instead of the concrete list.

Found this thread while I was looking for a solution to the exact sorun described in the original post. None of the answers met my situation entirely, however. Brody's answer was pretty close. Here is my situation and solution I found to it.

Say I have a function that returns IEnumerable, inside the function I may use a List for an internal backing store to generate my collection, but I only want callers to enumerate it's contents, derece add or remove. Accepting an interface kakım a parameter communicates a similar message "I need a collection of strings, don't worry though, I won't change it."

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 derece, copying it to an array, sorting that, clearing the IList, and re-adding the items.

In addition to the older code that doesn't know about generics, there are also a lot of cases where you know you have a list

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 can 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.

Dizilerde olduğu gibi özellikle C# IList Kullanımı kaç parça elemanla çkırmızıışılacağı belirtilmek zorunda bileğildir. Dizilerde başüstüneğu kabil eleman ekleme, silme fiillemleri uygulanabilir ve araya eleman eklenebilir. Rabıtlı listeler esenladığı avantajlardan dolayı henüz çok yeğleme edilir.

IList is an Interface, hamiş a class. If you want to initialize it, you need to initialize it to a class that implements IList, depending on your specific needs internally. Usually, IList is initialized with a List.

Whether you return an Interface or a concrete C# IList Nerelerde Kullanılıyor type depends upon what you want to C# IList Nedir 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 C# IList Kullanımı easily use a portion of the objects functionality (and of course whether you WANT them to be making full use of the object).

If you güç consider your method, determine that you probably won't be changing the return collection type, then it is probably safe to return a more exact type. If you aren't sure, or are afraid that if you change it in future you'll be breaking other people's code, then go more general.

Of course that only need apply to methods that are externally visible (i.e. public methods). I personally use interfaces even C# IList Nerelerde Kullanılıyor in internal code, but kakım you are able to change all the code yourself if you make breaking changes it's derece strictly necessary.

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 has to use List.

When talking about return types, the more specific you are, the more flexible callers yaşama be with it.

It doesn't affect the signature of the method, and is kaş in stone at compile time. You should instead be helping him get over his confusion about declaring his local like IList foo = new List - this is where his confusion clearly lies.

Report this page