Bir İnceleme C# ReadOnlyCollectionBase Nedir
Wiki Article
This class makes the underlying collection available through the InnerList property, which is intended for use only by classes that are derived directly from ReadOnlyCollectionBase. The derived class must ensure that its own users cannot modify the underlying collection.
akuaku 123k3333 gold badges174174 silver badges203203 bronze badges Add a comment
I had a client recently who wanted me to create a class that exposed a read-only collection through one of the class properties. While my client was willing to let other programs loop through that collection and retrieve items from the collection by position, he didn't want to let those programs add or remove items from the collection. In the Microsoft .Safi Framework 4.5 you dirilik use the read-only collections that Eric Vogel covered in an earlier C# Corner column, "The New Read-Only Collections in .Kemiksiz 4.5," to define that property. My client, however, was working with the .Kemiksiz Framework 4 and didn't intend to upgrade. Fortunately, in earlier versions of .Safi you dirilik also create a read-only class from an existing List just by calling your List's AsReadOnly method. However, if you need something special (a List that users dirilik add items to but derece remove items from), inheriting from the ReadOnlyCollectionBase class makes that very easy to do.
If you (Class A) continue to modify the underlying collection after you hand it out birli a ReadOnlyCollection then class B will see these changes, have any iterators invalidated, etc. and generally be open to any of the usual concurrency issues with collections.
Ancestors(IEnumerable, XName) Returns a filtered collection of elements that contains the ancestors of every node in the source collection. Only elements that have a matching XName are included in the collection.
You want to use the yield keyword. You loop through the IEnumerable list and C# ReadOnlyCollectionBase Kullanımı return the results with yeild. This allows the consumer to use the for each without modifying the collection.
Ancestors(IEnumerable) Returns a collection of elements that contains the ancestors of every node in the source collection.
Özellikle verilerin sınırlı bir sıraya gereğince aksiyonlenmesi müstelzim durumlarda, Queue kullanımı nominalm projelerinin verimliliğini zaitrabilir.
Note that the IReadOnlyList interface works similarly to List C# ReadOnlyCollectionBase Nerelerde Kullanılıyor and in fact sevimli be used in place of List when you want the elements of the list to be read-only. We will illustrate this with an example in the next section.
A ReadOnlyCollectionBase instance is always read-only. See C# ReadOnlyCollectionBase Nedir CollectionBase for a modifiable version of this class.
One possibility is to implement your own thread-safe variant of C# ReadOnlyCollectionBase Kullanımı the ReadOnlyCollection C# ReadOnlyCollectionBase Nerelerde Kullanılıyor to allow locked access, though this will be non-trivial and non-performant if you want to support IEnumerable, and it still
However, to be doubly sure, I'd like to return the collection kakım read-only, so that the calling code is unable to make changes to the collection, only view what's already there. Is this at all possible?
Average(IEnumerable, Func) Computes the average of a sequence of nullable Single values that are obtained by invoking a transform function on each element of the input sequence.
DefaultIfEmpty(IEnumerable, TSource) Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.