Detaylar, Kurgu ve C# IList Nasıl Kullanılır

How to refer to the locations in lower depths of a waterbody (such birli a lake)? more hot questions

Then the person calling the method is free to call it with any data type that is enumerable. This allows your code to be used in unexpected, but perfectly valid ways.

The less popular answer is programmers like to pretend their software is going to be re-used the world over, when infact the majority of projects will be maintained by a small amount of people and however birçok interface-related soundbites are, you're deluding yourself.

That way you take advantage if you yaşama, while still allowing the client flexibility in what they pass in.

Typically, a good approach is to use IList in your public facing API (when appropriate, and list semantics are needed), and then List internally to implement the API. This allows you to change to a different implementation of IList without breaking code that uses your class.

so its safety for you and freedom to the coder who is writing concrete implementation to change or add more functionality to his concrete class.

Dizilerde başüstüneğu üzere özellikle kaç pare elemanla çallıkışılacağı belirtilmek zorunda bileğildir. Dizilerde evetğu kabil eleman ekleme, silme fiillemleri uygulanabilir ve araya eleman eklenebilir. Rabıtalı listeler sağlamladığı avantajlardan dolayı daha çok tercih edilir.

Ask those people what they'd like the methods to return. Your question is fundamentally "how do I know what software to write?" You know by getting to know what problems your customer özgü to solve, and writing code that solves their problems.

For example, let's say you have a Person class and a Group class. A Group instance saf many people, so a List here would make sense. When I declare the list object in Group C# IList Kullanımı I will use an IList and instantiate it kakım a List.

Then later if you decide to convert the actual data store from a List to a Dictionary and expose the dictionary keys birli 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 problem! If you expose the List birli an IEnumerable you emanet comfortably predict that your collection is hamiş being modified externally. That is one of the powers of exposing List kakım any of the above interfaces.

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 C# IList Neden Kullanmalıyız it (e.g. the off chance a third party library returned C# IList Nerelerde Kullanılıyor a List that you needed to convert for another 3rd party library), you gönül do it this way.

In particular, IList lets you use the indexer, and add/remove items; things that IEnumerable don't let C# IList Neden Kullanmalıyız you do.

IEnumerable allows you to iterate through a collection. ICollection builds on this and also allows for adding and removing items. IList also allows for accessing and modifying them at a specific index. By exposing the one that you expect your consumer to work with, you are free to change your implementation. List happens to implement all three of those interfaces. If you expose C# IList Nasıl Kullanılır your property as a List or even an IList when all you want your consumer to have is the ability to iterate through the collection. Then they could come to depend on the fact that they güç modify the list.

Leave a Reply

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