C# ILIST NASıL KULLANıLıR NO FURTHER MYSTERY

C# IList Nasıl Kullanılır No Further Mystery

C# IList Nasıl Kullanılır No Further Mystery

Blog Article

Note that, if your API is only going to be used in foreach loops, etc, then you might want to consider just exposing IEnumerable instead.

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 sorun. However I think that especially in the case of collections that interfaces really shine.

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.

The following example demonstrates the implementation of the IList interface to create a simple, fixed-size list.

I tend to follow Jeffrey's advice for internal code, but for a public library, I would probably be more inclined to follow Eric's.

This level of abstraction goes the other direction when it belongs to method parameters. When you pass your list to a method that accepts IEnumerable you can be sure that your list is not going to be modified. When you are the person implementing the method and you say you accept an IEnumerable because all you need to do is iterate through that list.

You are most often better of using the most general usable type, in this case the IList or even better the IEnumerable interface, so that you kişi switch the implementation conveniently at a later time.

Now I am returning IList for the C# IList Kullanımı simple fact that I will then add this to my domain prototip what katışıksız a property like this:

And, if you don't even need everything in IList you dirilik always use IEnumerable too. With çağcıl compilers and processors, I don't think there is really any speed difference, so this is more just a matter of style.

It really comes down to the kind of functionality you need. I'd suggest using the List class in most cases. IList is best for when you need to make a custom array that could have some very specific rules that you'd C# IList Nedir like to encapsulate within a collection so you don't repeat yourself, but still want .Kemiksiz to recognize it kakım a list.

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 C# IList Nasıl Kullanılır 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.

Bu örnekte, Student adında bir derslik ve StudentCollection adında CollectionBase dershaneından türeyen özel bir derme sınıfı oluşturduk.

And, if you used a generic implementation, you would only be able to use a method that works for C# IList Kullanımı any object only with objects of a specific type.

The other general reason for using interfaces is to expose the asgari C# IList Nasıl Kullanılır amount of knowledge necessary to the user of an object. Consider the (contrived) case where I have a data object that implements IList.

Report this page