Benim C# IList Neden Kullanmalıyız Başlarken Çalışmak

Wiki Article

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

Aşağıdaki şekilde Kisi adında oluşturduğumuz dershaneı oluşturduğumuz liste nesnesine ekleyelim.

Using IList instead of List makes writing unit tests significantly easier. It allows you to use a 'Mocking' library to pass and return data.

Eric LippertEric Lippert 656k182182 gold badges1.3k1.3k silver badges2.1k2.1k bronze badges 14 12 How do you know what the caller needs though. For instance I was switching one of my return types to a IList then I well I am probably just going to enumerate over them anyways lets just return an IEnumberable.

The preceeding line of code will work, but you will only have the members of IList available to you instead of the full set from whatever class you initialize.

Convert your IList into List or some other generic collection and then you can easily query/sort it using System.Linq namespace (it will supply bunch of extension methods)

 

IList is an Interface, 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.

but my C# IList Nedir sıkıntı is that i am derece understanding what is its use and when to use it . So i hope that anyone güç help me . Thanks .

If you kişi consider your method, determine that you probably won't be changing the return collection type, then it is probably C# IList Kullanımı safe to return a more exact type. If you aren't sure, or are afraid that if you change it in future you'll be breaking C# IList Neden Kullanmalıyız other people's code, then go more general.

However, this makes the method more fragile, bey any change to the returned object type may break the calling code. In practice though, that generally C# IList Nedir isn't a major sıkıntı.

rajeshrajesh 39133 silver badges22 C# IList Nedir bronze badges 1 8 Excellent, clear answer, which I marked bey helpful. However, I would add that for most developers, most of the time, the tiny difference in izlence size and performance is hamiş 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.

So I have been playing around with some of my methods on how to do this. I am still not sure about the return type(if I should make it more concrete or an interface).

Report this wiki page