EN IYI TARAFı C# ILIST KULLANıMı

En iyi Tarafı C# IList Kullanımı

En iyi Tarafı C# IList Kullanımı

Blog Article

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

Arec BarrwinArec Barrwin 61.8k99 gold badges3030 silver badges2525 bronze badges 14 71 I have to disagree with your sardonic answer. I don't totally disagree with the sentiment of over-architecture being a real sıkıntı. However I think that especially in the case of collections that interfaces really shine.

I think that, ideally, the .Kupkuru Framework would include a static sorting method that accepts an IList, but the next best thing is to create your own extension method. It's derece too hard to create a couple of methods that will allow you to sort an IList kakım you would a List.

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.

The accepted answer by @DavidMills is quite good, but I think it sevimli be improved upon. For one, there is no need to define the ComparisonComparer class when the framework already includes a static method Comparer.Create(Comparison). This method yaşama be used to create an IComparison on the fly.

I would turn the question around a bit, instead of justifying why you should use the interface over the concrete implementation, try to justify why you would use the concrete implementation rather than the interface. If you hayat't justify it, use the interface.

Inside the method, you should use var, instead of IList or List. When your veri source changes to come from a method instead, your onlySomeInts method will survive.

Don't you know in advance if your method needs a list that gönül take additional members; don't you specify that in the method signature? What exactly were you going to do if you were passed a read only list like C# IList Nasıl Kullanılır int[]?

In VS2008, when I click on the service reference and C# IList Neden Kullanmalıyız select "Configure Service Reference", there is an option to choose how the client de-serializes lists returned from the service.

Modülerlik: Yazılım projelerinde modüler bir yaklaşım sunarak şifre yineını azaltır ve bakımı kolaylaştırır.

I know that IList is the interface and List is the concrete type but I still don't know when to use each one. What I'm doing now is if I don't need the Sort or FindAll C# IList Nedir methods I use the interface. Am I right? Is there a better way to decide when to use the interface or the concrete type?

Benefit of using an Interface is that you get to implement your functionality or better yet, the only functionality you require. So, if iteration/enumeration is required only, then there is no C# IList Neden Kullanmalıyız need for the Sort, Add methods.

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 a specific type.

IEnumerable allows you to iterate through a collection. ICollection builds on this and also allows for adding and removing items. IList also allows for accessing and modifying them at a specific index. By exposing the one that you expect your consumer to work with, you are free to change your implementation. List happens to implement all three of those interfaces. If you expose your property bey a List or even an IList when all you want your consumer to have is the ability to iterate through the collection. Then they could come to depend on the fact C# IList Kullanımı that they sevimli modify the list.

Report this page