En iyi Tarafı C# ReadOnlyCollectionBase Kullanımı
En iyi Tarafı C# ReadOnlyCollectionBase Kullanımı
Blog Article
I'm going to start by being perverse and pretending that the ReadOnlyCollectionBase class doesn't exist. If that were true and you wanted to create a read-only collection, you'd create a class that only returned an Enumerator object. A class that just implements IEnumerable is, according to my definitions, a read-only collection class. Generating that GetEnumerator object from your class isn't difficult if you keep your class veri in a class that itself implements IEnumerable -- a List, for instance. If you do keep your veri in a List, then you dirilik use the List GetEnumerator method to retrieve an Enumerator object, which you güç then return to the program using your class. Listing 1 özgü the basics of this class (I've called it PhvReadOnlyList).
read this question and don't want to read swathes of API docs yet still understand what the intent is. It's also really important to minimize code clutter - so things like ReadOnlyCollection are dubious at best.
I was thinking about creating a new class derived from ReadOnlyCollection (class ReadOnlyMyCollection : ReadOnlyCollection or class ReadOnlyMyCollection : ReadOnlyCollection) which returns the read-only wrapper class (ReadOnlyMyItem) through the square brackets operator, but either the square brackets operator is hamiş marked birli virtual and the return type is derece even the same or the constructor doesn't have an IList of the wrapper class (IList) to use. Should I build the class (ReadOnlyMyCollection) from scratch?
Bey you suggest, my intention is only to protect the collection from direct modification; the collection members are hamiş going to be modified (or needed to be modified).
Bu örnekte, articles1 ve articles2 dizileri aynı makale başlıklarına aynı C# ReadOnlyCollectionBase Temel Özellikleri sırada sahip olduğundan, CompareTo metodu 0 döndürerek bu dizilerin yapısal olarak hemayar olduğunu belirtir.
Is there any reason to expose an internal collection birli a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection?
4) If you suddenly realise that your collection of choice is inefficient, you gönül change the underlying collection type without having to go and change all the places where it got passed kakım a parameter (gönül you imagine the trouble you might have with, say, List?)
After hopefully understanding your question, i think you have to distinguish between what you create and manage within your class and what you make available to the outside world.
This article discusses these read-only immutable collection types in .Safi Core and how you güç work with them in C# ReadOnlyCollectionBase Kullanımı C#.
API Entegrasyonu: Dış API'lerden düzenınan verileri gitmek ve yönetmek yürekin kullanılabilir, bu da icraat arası done münasebetini kolaylaştırır.
Implementers are encouraged to extend this base class instead of creating their own. Members of this base class are protected and are intended to be used through a derived class only.
First, some definitions: A read-only collection is like a List in that it C# ReadOnlyCollectionBase Nasıl Kullanılır supports iterating through the items with a For…Each loop or retrieving an item in the collection by position.
If you need to do more than the InnerList property will let you, the base class Items property will hand you back the C# ReadOnlyCollectionBase Nedir whole List. Bey I said, creating a read-only collection class in .
Key values are compared by using a specified comparer, C# ReadOnlyCollectionBase Nerelerde Kullanılıyor and the elements of each group are projected by using a specified function.