C# IList Nerelerde Kullanılıyor No Further Mystery

You cannot predict the future. Assuming that a property's type will always be beneficial birli a List is immediately limiting your ability to adapt to unforeseen expectations of your code.

Same principle kakım before, reversed. Offer the bare minimal that your caller requires. If the caller only requires the ability to enumerate the sequence, only give them an IEnumerable.

So I came across an interesting sorun today. We have a WCF web service that returns an IList. Hamiş really a big deal until I wanted to sort it.

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.

Important Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

use LINQ to perform the conversion of your existing List when you return it - but it would be better to just create a more appropriate type to start with, kakım shown above.

From my reading I think I could have used IEnumberable instead of IList since I am just looping through stuff.

And, if you don't even need everything in C# IList Nasıl Kullanılır IList you hayat always use IEnumerable too. With çağdaş compilers and processors, I don't think there is really any speed difference, so this is more C# IList Kullanımı just a matter of style.

List implements those nine methods (hamiş including extension methods), on bütünüyle of that it saf about 41 public methods, which weighs in your consideration of which one to use in your application.

On the other hand, when returning an object out of a function, you want C# IList Nasıl Kullanılır to give the user the richest possible seki of operations without them having to cast around. So in that case, if it's a List internally, return a copy birli a List.

The most important case for using interfaces C# IList Neden Kullanmalıyız over implementations is in the parameters to your API. If your API takes a List parameter, then anyone who uses it özgü to use List.

If you use the concrete type all callers need to be updated. If exposed kakım IList the caller doesn't have to be changed.

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 data C# IList Nerelerde Kullanılıyor object that implements IList.

Leave a Reply

Your email address will not be published. Required fields are marked *