C# IList Nerelerde Kullanılıyor Için 5-İkinci Trick

Wiki Article

I know there katışıksız 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.

But far more importantly, if you are accepting an IList as a parameter you'd better be careful, because IList and List do not behave the same way. Despite the similarity in name, and despite sharing an interface

So I came across an interesting mesele today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort 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 not, copying it to an array, sorting that, clearing the IList, and re-adding the items.

Use whatever you want. It's your method. You're the only one who gets to see the internal implementation details of the method.

You yaşama have an instance of an interface, C# IList Nasıl Kullanılır but you need to initialize it with a class that implements that interface such kakım:

The class name List may be changed in next .net framework but the interface is never going to change bey interface is contract.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

If you started with a concrete type and you decided to change to another one, even if it uses the same interface, you're going to break someone else's code unless you started off with an interface or abstract base type. Share Improve this answer Follow

If you hayat 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 C# IList Kullanımı change it in future you'll be breaking other people's code, then go more general.

Kişi you please provide me some link to a class that implements IList interface or provide me a code that at least implements Add and Remove methods?

Less ridiculous C# IList Kullanımı way to prove that an Ascii character compares equal with itself in Coq more hot questions

If you use the concrete type all callers need to be updated. If exposed kakım IList the caller C# IList Kullanımı doesn't have to be changed.

Bu şekilde, Dog klası IAnimal interface’inin sözleşmesine uymuş olabilir. Aynı şekilde, değişik hayvan sınıfları da IAnimal interface’ini uygulayabilir. Örneğin, aşağıdaki kodda C# IList Nerelerde Kullanılıyor bir Cat dershaneı tanımladım ve IAnimal interface’ini uyguladım.

Report this wiki page