Greatest Kılavuzu C# IList Kullanımı için

Wiki Article

I toyed with writing an extension method, also with inheriting from IList and implementing my own Sort() method as well birli casting to a List but none of these seemed overly elegant.

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.

Also, it casts IList to IList which özgü the potential to be dangerous. In most cases that I have seen, List which implements IList is used behind the scenes to implement IList, but this is derece guaranteed and hayat lead to brittle code.

Bearing this in mind, it makes most sense to pass types with the least number of external dependencies possible and to return the same. However, this could be different depending on the visibility of your methods and their signatures.

swilliamsswilliams 48.6k2727 gold badges101101 silver badges130130 bronze badges 3 5 why derece make it a just a List in the first place? I still don't understand why bonus you get from making it a IList then in the constructor you make it into a List

The Liskov Substitution Principle (simplified) states that a derived type should be able to be used in place of a base type, with no additional preconditions or postconditions.

In case of using IList, the caller is always guareented things to work, and the implementer is free to change the underlying collection to any alternative concrete implementation of IList

IList is an Interface, C# IList Neden Kullanmalıyız not 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.

; C# IList Neden Kullanmalıyız 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 Nasıl Kullanılır 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 C# IList Neden Kullanmalıyız a subclass (in this case the interface) and assign the concrete class object to it.

Then later if you decide to convert the actual veri store from a List to a Dictionary and expose the dictionary keys bey the actual value for the property (I have had 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 problem! If you expose the List kakım an IEnumerable you hayat comfortably predict that your collection is derece being modified externally. That is one of the powers of exposing List kakım any of the above interfaces.

 

Is IList a good fit for your organisation? If a colleague asks you to change a method signature to use IList instead of List, ask them how they'd add an element to an IList. If they don't know about IsReadOnly (and most people don't), then don't use IList. Ever.

And, if you used a generic implementation, you would only be able to use a method that works for any object only with objects of C# IList Nasıl Kullanılır a specific type.

The other general reason for using interfaces is to expose the asgari amount of knowledge necessary to the user of an object. Consider the (contrived) case where I have a data object that implements IList.

Report this wiki page