C# ILIST NEDIR SEçENEKLER

C# IList Nedir Seçenekler

C# IList Nedir Seçenekler

Blog Article

I know there katışıksız been a lot of posts on this but it still confuses me why should you pass in an interface like IList and return an interface like IList back instead of the concrete list.

Today, you almost always use IList, the primary reason for IList to still be around is for reasons of backwards compatibility.

Elemanların Sıralı Yapkaloriı Müdafaa: IList, elemanların eklenme sırasını korur. Bu özellik, data yapısının sıralı olmasını ve programın beklentilerine orantılı çtuzakışmasını esenlar.

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.

Use whatever you want. It's your method. You're the only one who gets to see the internal implementation details of the method.

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 dirilik't justify it, use the interface.

Object güç be a T too. Doing this will save you headache if you decide to use a Stack or some C# IList Nerelerde Kullanılıyor other veri structure further down the road. If all you need to do in the function is foreach through it, IEnumerable is really all you should be C# IList Nerelerde Kullanılıyor asking for.

If your methods form part of an interface, the methods will need to be defined using types available to that interface.

For example, let's say you have a C# IList Nerelerde Kullanılıyor Person class and a Group class. A Group instance başmaklık many people, so a List here would make sense. When I declare the list object in Group C# IList Nasıl Kullanılır 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 düzgülü static method. It is quite ugly and verbose unfortunately.

On the other hand, when returning an object out of a function, you want to give the user the richest possible kaş of operations without them having to cast around. So in that case, if it's a List internally, return a copy as a List.

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 C# IList Nerelerde Kullanılıyor hayat do it this way.

If you're just enumerating over the values, you should be using IEnumerable. Every type of datatype that yaşama hold more than one value implements IEnumerable (or should) and makes your method hugely flexible.

The other general reason for using interfaces is to expose the minimum amount of knowledge necessary to the user of an object. Consider the (contrived) case where I have a veri object that implements IList.

Report this page