C# ILIST NEDEN KULLANMALıYıZ IçIN 5-İKINCI TRICK

C# IList Neden Kullanmalıyız Için 5-İkinci Trick

C# IList Neden Kullanmalıyız Için 5-İkinci Trick

Blog Article

The above is an IList itself kakım this is what seems to be the standard to use with nhibernate. Otherwise I might have returned IEnumberable back but not sure. Still, I birey't figure out what the user would 100% need(that's where returning a concrete has an advantage over).

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

The less popular answer is programmers like to pretend their software is going to be re-used the world over, when infact the majority of projects will be maintained by a small amount of people and however nice interface-related soundbites are, you're deluding yourself.

It's more nuanced than that. If you are returning an IList as part of the public interface to your library, you leave yourself interesting options to perhaps make a custom list in the future.

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.

IList on the other hand is an Interface. Basically, if you want to create your own custom List, say a list class called BookList, then you dirilik use the Interface to give you basic methods and structure to your new class. IList is for when you want to create your own, special sub-class that implements List.

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

Upgrade to Microsoft Edge to take C# IList Nasıl Kullanılır advantage of the latest features, security updates, and technical support.

For example, let's say you have a Person class and a Group class. A Group instance katışıksız many C# IList Nasıl Kullanılır people, so a List here would make sense. When I declare C# IList Neden Kullanmalıyız the list object in Group I will use an IList and instantiate it bey a List.

The Cast function is just a reimplementation of the extension method that comes with 3.5 written birli a normal static method. It is C# IList Kullanımı quite ugly and verbose unfortunately.

If you're working within a single method (or even in a single class or assembly in some cases) and no one outside is going to see what you're doing, use the fullness of a List. But if you're interacting with outside code, like when you're returning a list from a method, then you only want to declare the interface without necessarily tying yourself to a specific implementation, especially if you have no control over who compiles against your code afterward.

I leave this up to show that if you needed to do it (e.g. the off chance a third party library returned a List that you needed to convert for another 3rd party library), you yaşama do it this way.

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

Have children's car seats not C# IList Nedir been proven to be more effective than seat belts alone for kids older than 24 months?

Report this page