Benim C# IList Kullanımı Başlarken Çalışmak
Benim C# IList Kullanımı Başlarken Çalışmak
Blog Article
You cannot predict the future. Assuming that a property's type will always be beneficial kakım a List is immediately limiting your ability to adapt to unforeseen expectations of your code.
From this it follows that your method implementation emanet represent its local variables however you wish. The implementation details are hamiş exposed. Leaving you free to change your code to something better without affecting the people calling your code.
Also, it casts IList to IList which saf 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 sevimli 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.
In addition to the older code that doesn't know about generics, there are also a lot of cases where you know you have a list
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The cost to do this is minimal, why hamiş save yourself the headache later? It's what the interface principle is all about.
However using a concrete implementation and List on a class that changes, could cause the calling code to need to be changed bey well. This is because a class adhering to IList guarantees a certain behavior that is not guaranteed by a concrete type using List. Also having the power to do something like modify the default implementation of List on a class Implementing IList for C# IList Nasıl Kullanılır say the .Add, .Remove or any other IList method gives the developer a lot
If the parameter type is IList, then the caller katışıksız much more freedom, and emanet use classes you never heard about, C# IList Nasıl Kullanılır which may not even have existed when your code was written.
Then later if you decide to convert the actual data 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 C# IList Neden Kullanmalıyız come to expect that their changes will be reflected inside of your class will no longer have that capability. That's a big sıkıntı! If you expose the List birli an IEnumerable you güç comfortably predict that your collection is not being C# IList Neden Kullanmalıyız modified externally. That is one of the powers of exposing List bey any of the above interfaces.
Do the decoupling capacitors act kakım capacitive load to the opamp which is used to C# IList Nerelerde Kullanılıyor make a virtual gorund?
Returning a read-only interface such birli IEnumerable is often the way to go for veri-retrieval methods. Your consumer yaşama project it into a richer type bey-needed.
This will help if you decide to change the implementation of your class later to use a different concrete class. In that case the users of your library won't need to update their code since the interface doesn't change.
Encapsulation relies on telling clients birli little about the implementation of your class as possible. If you return a concrete List, you hayat't then change to some other better type without forcing all of your clients to re-compile/update.