GREATEST KıLAVUZU C# ILIST KULLANıMı IçIN

Greatest Kılavuzu C# IList Kullanımı için

Greatest Kılavuzu C# IList Kullanımı için

Blog Article

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

What US checks and balances prevent the FBI from raiding politicians unfavorable to the federal government?

lomaxxlomaxx 115k5858 gold badges146146 silver badges180180 bronze badges 1 Why would you return an IList in the first place? From a WCF service?

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

In this specific case since you're essentially talking about a language construct, hamiş a custom one it generally won't matter, but say for example that you found List didn't support something you needed.

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

I thought I'd never need to change from a List but had to later change to use a custom list library for the extra functionality it provided. Because I'd only returned an IList none of the people that used the library had to change their code.

Don't you know in advance if your method needs a list that sevimli take additional members; don't you specify that in the method signature? What exactly were you going to do if you were passed a read only list C# IList Neden Kullanmalıyız like int[]?

For collections you should aim to use IEnumerable where possible. This gives the most flexibility but is derece always suited.

Then later if you decide to convert the actual veri store from a List to a Dictionary and expose the dictionary keys bey the actual value for the property (I have had to do exactly this before). Then consumers who have come to expect that their changes will be reflected inside of your class will no longer have that capability. That's a big mesele! If you expose the List as an IEnumerable you kişi comfortably predict that your collection is derece being modified externally. That is one of the powers of exposing List bey any of the C# IList Kullanımı above interfaces.

I know that IList is the interface and List is the concrete type but I still don't know when to use each one. What I'm doing now is if I don't need the Sort or FindAll methods I C# IList Neden Kullanmalıyız use the interface. Am I right? Is there a better way to decide when to use the interface or the concrete type?

Coming soon: Throughout 2024 we will be phasing out GitHub Issues birli the feedback mechanism for content and C# IList Neden Kullanmalıyız replacing it with a new feedback system. For more information see: .

If you use a concrete implementation of list, another C# IList Kullanımı implementation of the same list will derece be supported by your code.

So I have been playing around with some of my methods on how to do this. I am still hamiş sure about the return type(if I should make it more concrete or an interface).

Report this page