C# ReadOnlyCollectionBase Nerelerde Kullanılıyor Aptallar için

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 data in a class that itself implements IEnumerable -- a List, for instance. If you do keep your veri in a List, then you yaşama use the List GetEnumerator method to retrieve an Enumerator object, which you can then return to the izlence using your class. Listing 1 saf the basics of this class (I've called it PhvReadOnlyList).

Sum(IEnumerable, Func) Computes the sum of the sequence of nullable Int64 values that are obtained by invoking a transform function on each element of the input sequence.

I avoid using ReadOnlyCollection as much birli possible, it is actually considerably slower than just using a alışılagelen List.

then sure, no harm will be done if you just return the collection directly. I generally try to be a bit more paranoid than that though.

Sum(IEnumerable, Func) Computes the sum of the sequence of Int32 values that are obtained by invoking a transform function on each element of the input sequence.

Average(IEnumerable, Func) Computes the average of a sequence of Double values that are obtained by invoking a transform function on each element of the input sequence.

Thous leads that your consumer classes are just awaiting interfaces instead of concrete classes and so you kişi change the concrete implementation at a later stage without breaking your public contract C# ReadOnlyCollectionBase Kullanımı (due to performance reasons use an List instead of a LinkedList or vice versa).

; but this question convinced me to go ahead and announce an open source project I've had in the works for a while (still a work in progress, but there's some useful C# ReadOnlyCollectionBase Temel Özellikleri stuff in there), which includes an IArray interface (and implementations, naturally) that I think captures exactly what you want here: an indexed, read-only, even covariant

Exposing the array directly to those using it means that C# ReadOnlyCollectionBase Nasıl Kullanılır they sevimli modify it - this is a violation of encapsulation and data hiding.

IDictionary tabanlı nesnelerin nasıl sıralama yaptığı ile dayalı şu C# ReadOnlyCollectionBase Nedir kodu yazarak bir test yaparsak:

However, if you want to access the elements of the IEnumerable collection by index or you want to retrieve the number of elements in the collection, you should use IReadOnlyList kakım shown below.

SingleOrDefault(IEnumerable, Func) Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.

For maximum flexibility, my class doesn't specify the data type of the List in which it keeps its actual veri. Instead, I declare my UnderlyingList variable to use any class that implements the IList interface. I've also made this a generic class: Rather than specify what kind of List I'm working with, I've forced my class to be instantiated with a data type that the code refers to kakım T. Wherever in my code I need to specify my List's veri type, I just use the C# ReadOnlyCollectionBase Nasıl Kullanılır T reference. Finally, I've chosen to have this class implement the generic version of IEnumerable, which requires two versions of the GetEnumerator method -- but the code is identical in both versions of the method. Listing 1. The basics of the PhvReadOnlyList class.

Sum(IEnumerable, Func) Computes the sum of the sequence of nullable Decimal values that are obtained by invoking a transform function on each element of the input sequence.

Leave a Reply

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