C# READONLYCOLLECTIONBASE NERELERDE KULLANıLıYOR - GENEL BAKış

C# ReadOnlyCollectionBase Nerelerde Kullanılıyor - Genel Bakış

C# ReadOnlyCollectionBase Nerelerde Kullanılıyor - Genel Bakış

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

Tip You birey loop over the ReadOnlyCollection using the foreach-loop. A for-loop is also possible with the Count property and indexer.

In a multithreaded environment, this is even more of an issue, bey one thread güç make changes to the data while another is trying to read veri - you dirilik easily get inconsistent data in different threads.

List's are a very common datastructure, but creating them in this fashion involves quite a bit of syntactic noise - and it dirilik easily get even worse (e.g.

Imagine we have existing model with Organization => Employees already set up. To apply this technique, we need to change Organization C# ReadOnlyCollectionBase Nerelerde Kullanılıyor manken a bit:

Also, native collections dirilik't be enhanced with extra veri. You'll recognize this next time you find that you pass in (Collection, Extra) to more than one or two methods. It indicates that "Extra" belongs with the object containing your collection.

If you're tolerant of some "noise" in your own encapsulated code then I would suggest wrapping a T[] array and exposing an IList which happens to be a ReadOnlyCollection:

An immutable object is defined birli an object that cannot be changed after it başmaklık been created. Not all collections are immutable, but you yaşama use the read-only collection types in .

Note that the IReadOnlyList interface works similarly to List and in fact gönül C# ReadOnlyCollectionBase Nasıl Kullanılır 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.

jasonjason 240k3535 C# ReadOnlyCollectionBase Nedir gold badges431431 C# ReadOnlyCollectionBase Kullanımı silver badges529529 bronze badges 2 Really, IEnumerable has the same issue as IList in principle, but implementing a single extra method (and a single extra property on the enumerator) was derece C# ReadOnlyCollectionBase Temel Özellikleri considered a burden, compared to the dozens you would need for IList (which List and indeed ReadOnlyCollection do implement, separately from their generic collection interfaces)

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 as shown below.

If the collection is small then a safe and easy way to get around this sorun is to return a copy instead.

If you need to do more than the InnerList property will let you, the base class Items property will hand you back the whole List. Bey I said, creating a read-only collection class in .

A method like PayUnpaidInvoices being present may lead to second guessings about what "regular" collection methods like Remove and Clear might do. Will it just remove invoices from the in-memory collection, or will it delete them from the database?

Report this page