TEMEL İLKELERI C# ILIST NASıL KULLANıLıR

Temel İlkeleri C# IList Nasıl Kullanılır

Temel İlkeleri C# IList Nasıl Kullanılır

Blog Article

Kısaca IEnumerable binasında filtreleme mesleklemleri memory bile örgülırken, IQueryable da veritabanından düver filtrelenmiş verileri elde ederiz.

IList is an interface so you birey inherit another class and still implement IList while inheriting List prevents you to do so.

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 kişi lead to brittle code.

That way you take advantage if you kişi, while still allowing the client flexibility in what they pass in.

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

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 kişi be sure that your list is derece 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.

You may not ever need that option, but it's an argument. I think it's the entire argument for returning the interface instead of the concrete type. It's worth mentioning, but in this case it saf a serious flaw.

OdedOded 496k101101 gold badges890890 silver badges1k1k bronze badges Add a comment  

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

Then later if you decide to convert the actual data 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 C# IList Neden Kullanmalıyız inside of your class will no longer have that capability. That's a big problem! If you expose the List bey an IEnumerable you yaşama comfortably predict that your collection is hamiş being modified externally. That is one of the powers of exposing List birli any of the above interfaces.

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

rajeshrajesh 39133 silver badges22 bronze badges 1 8 Excellent, clear answer, which I marked birli helpful. However, I would add that for most C# IList Nasıl Kullanılır developers, most of the time, the tiny difference in izlence size and performance is not worth worrying about: if in doubt, just use a List.

for your return types. This gives your callers the most flexibility in passing in types to your methods and the most opportunities to cast/reuse the return values.

Encapsulation relies on telling clients as little about the implementation of your class birli possible. If you return a concrete List, you hayat't then change to some other better type without forcing all of your clients to re-compile/update.

Report this page