Bir İnceleme C# IList Neden Kullanmalıyız

I know there saf 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 mesele 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.

Important Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Your functions above only care about being able to iterate over a list. Ideally they shouldn't need to know who implements that list or how they implement it.

ToList first? How about returning it? I don't understand what you mean by "method will survive" when using vars? I know it will be a bit more work but won't you just have to change that to the new type kakım well? So maybe IList to IList?

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 yaşama be sure that your list is not going to be modified. When you are the person implementing the method and you C# IList Kullanımı say you accept an IEnumerable because all you need to do is iterate through that list.

StuartLCStuartLC 106k1818 gold badges216216 silver badges289289 bronze badges Add a comment  

Now I am returning IList for the simple fact that I will then add this to my domain manken what saf a property like C# IList Nasıl Kullanılır this:

In most cases, if C# IList Nasıl Kullanılır you are using a List and you think you could use a narrower interface instead - why hamiş IEnumerable? This is often a better fit if you don't need to add items. If you need to add to the collection, use the concrete type, List.

class Kisi string ad; string soyad; public string Ad get return ad; grup ad = value; public string Soyad get return soyad; kaş soyad = value;

Obviously if you are being asked which you use in C# IList Nasıl Kullanılır an interview, you say IList, smile, and both look pleased at yourselves for being so clever. Or for a public facing API, IList. Hopefully you get my point.

Brad LeachBrad Leach 17k1818 gold badges7373 silver badges8888 bronze badges 1 3 It will create a new enumerable, which may derece be desirable in some scenarios. You cannot sort an IList in-place through the interface C# IList Kullanımı except by using ArrayList.Adapter method in my knowledge.

I have two ILists of the same type returned by NHibernate and have emerged the two IList into one, hence the need for sorting.

This works, because only the outer list is created in the first place. You yaşama then insert individual items that are compatible with IList:

Leave a Reply

Your email address will not be published. Required fields are marked *