En iyi Tarafı C# IList Nedir
En iyi Tarafı C# IList Nedir
Blog Article
Else use List. You can't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List hamiş an IList, else you are breaking Liskov substitution principle: "if S is a subtype of T, then objects of type T may be replaced with objects of type S"
Hordaki şekilde Kisi adında oluşturduğumuz derslikı oluşturduğumuz liste nesnesine ekleyelim.
lomaxxlomaxx 115k5858 gold badges146146 silver badges180180 bronze badges 1 Why would you return an IList in the first place? From a WCF service?
Sıfır zir hudutına ehil yalnızca boyutlu diziler kendiliğinden olarak uygular IList. Bu, diziler ve özge koleksiyon türleri ortada yineleme gitmek sinein aynı kodu kullanabilen genel yöntemler oluşturmanıza olanak tanılamar.
The preceeding line of code will work, but you will only have the members of IList available to you instead of the full takım from whatever class you initialize.
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.
class Kisi string ad; string soyad; public string Ad get return ad; set ad = value; public string Soyad get return soyad; set soyad = value;
Matthew WatsonMatthew Watson 108k1111 gold badges170170 silver badges290290 bronze badges 2 2 This is trivially true for every interface. If you want to follow through with your argument, than you could argue to never use any interface C# IList Kullanımı at all, because some implementation of it might throw.
For example, let's say you have a Person class and a Group class. A Group instance başmaklık C# IList Nerelerde Kullanılıyor many people, so a List here would make sense. When I declare the list object in Group I will use an IList and instantiate C# IList Nerelerde Kullanılıyor it kakım a List.
Modülerlik: Yazılım projelerinde modüler bir yaklaşım sunarak harf geneını azaltır ve hizmetı kolaylaştırır.
However, this makes the method more fragile, birli any C# IList Nasıl Kullanılır change to the returned object type may break the calling code. In practice though, that generally isn't a major mesele.
Then I looked in my view(mvc) and found that I actually needed the count method birli I needed to use a for loop. So in my own application I under estimated what I actually needed how do you anticipate what someone else will need or hamiş need.
You dirilik pass a plain array to something which accepts an IList parameter, and then you dirilik call IList.Add() and will receive a runtime exception:
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 C# IList Nedir object that implements IList.