C# IList Kullanımı Günlükler

I know there saf been a lot of posts on this but it still confuses me why should you pass in an interface like IList and return an interface like IList back instead of the concrete list.

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

Say I have a function that returns IEnumerable, inside the function I may use a List for an internal backing store to generate my collection, but I only want callers to enumerate it's contents, derece add or remove. Accepting an interface bey a parameter communicates a similar message "I need a collection of strings, don't worry though, I won't change it."

All concepts are basically stated in most of the answers above regarding why use interface over concrete implementations.

Kemiksiz 4.6 (and it will likely be caught by the compiler). But there gönül be more insidious cases, such kakım passing a C# array kakım a IList. I am derece sure everyone is aware arrays implement IList, which means support for Add should hamiş be assumed.

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.

+1 for all of us who hate it when a small app C# IList Nedir is loaded with interfaces and clicking on "find definition" takes us somewhere OTHER than the source of the problem... Yaşama I borrow the phrase "Architecture Astronauts"? I kişi see it will come in handy.

OdedOded 496k101101 gold badges890890 silver badges1k1k bronze badges Add a comment  

Whether you return an Interface or a concrete type depends upon C# IList Kullanımı what you want to let your callers do with the object you created -- this is an API design decision, and there's no hard and fast rule. You have to weigh their ability to make full use of the object against their ability to easily use a portion of the objects functionality (and of course whether you WANT them to be making full use of the object).

The Cast function is just a reimplementation of the extension method that comes with 3.5 written kakım a olağan static method. It is quite ugly and verbose unfortunately.

Yaşama you please provide me some link to a class that implements IList interface or provide me a code that at least implements Add and Remove methods?

Is IList a good fit for your organisation? If a colleague asks you to change a method signature to use IList instead of List, ask C# IList Nasıl Kullanılır them how they'd add an element to an IList. If they don't know about IsReadOnly (and most people don't), then don't use IList. Ever.

In collections of contiguous elements, such bey lists, the elements that C# IList Nerelerde Kullanılıyor follow the insertion point move down to accommodate the new element.

There is a complication though that dynamic can't see explicit implementations, C# IList Nerelerde Kullanılıyor so something yaşama implement IList and IList-of-T and yet still be completely unusable from dynamic.

Leave a Reply

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