DEğIL HAKKıNDA DETAYLAR BILINEN C# ILIST KULLANıMı

Değil Hakkında Detaylar bilinen C# IList Kullanımı

Değil Hakkında Detaylar bilinen C# IList Kullanımı

Blog Article

It's always best to use the lowest base type possible. This gives the implementer of your interface, or consumer of your method, the opportunity to use whatever they like behind the scenes.

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

Also, it casts IList to IList which katışıksız the potential to be dangerous. In most cases that I have seen, List which implements IList is used behind the scenes to implement IList, but this is not guaranteed and dirilik lead to brittle code.

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.

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.

List is a specific implementation of IList, which is a container that emanet be addressed the same way bey a linear array T[] using an integer index. When you specify IList birli the type of the method's argument, you only specify that you need certain capabilities of the container. For example, the interface C# IList Nasıl Kullanılır specification does not enforce a specific veri structure to be used.

List implements IList, and so sevimli be assigned to the variable. There are also other types that also implement IList.

From my reading C# IList Nasıl Kullanılır 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 Neden Kullanmalıyız 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 Neden Kullanmalıyız just a matter of style.

Modülerlik: Mukayyetm projelerinde modüler bir yaklaşım sunarak kod tekrarını azaltır ve hizmetı kolaylaştırır.

This argument only works if you write your own implementation of IList from sratch (or at least without inheriting 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 can do it this way.

for your return types. This gives C# IList Neden Kullanmalıyız your callers the most flexibility in passing in types to your methods and the most opportunities to cast/reuse the return values.

Bu şekilde, Dog sınıfı IAnimal interface’inin sözleşmesine uymuş olur. Aynı şekilde, diğer hayvan sınıfları da IAnimal interface’ini uygulayabilir. Örneğin, dundaki kodda bir Cat dershaneı tanımladım ve IAnimal interface’ini uyguladım.

Report this page