Avoid shared lists in the base class. Do I have to follow one rule for a dictionary?

I was recommended to change the property List<string>to Collection<string>in the base class, because it is more appropriate for inheritance.

The following was mentioned in this "rule": https://msdn.microsoft.com/en-us/library/ms182142.aspx

System.Collections.Generic.List is a general collection that is intended to be executed, not inherited. System.Collections.Generic.List does not contain virtual members, which simplifies changing the behavior of an inherited class. The following general collections are for inheritance and should be set instead of System.Collections.Generic.List.

System.Collections.ObjectModel.Collection<T>

System.Collections.ObjectModel.ReadOnlyCollection<T>

System.Collections.ObjectModel.KeyedCollection<TKey, TItem>

A similar rule applies to Dictionary<string, string>?

, System.Collections.Generic. , , , Lists.

BTW, Dictionary , ( ModelState). , .


Dictionary<string, string> , ?

KeyedCollection, , .

+4
2

, , , .

msdn , , :

public class MyCollection : Collection<MyType>

Collection<T> , , , : ClearItems, InsertItem, RemoveItem SetItem. List<T>, - ( ToString, Equals GetHashCode).

, , List/Dictionary/Collection . .

, , , , , . , - , , System.Collections.Generic.

, , , .

: KeyedCollection ( ). KeyedCollection , KeyedCollection.

+2

Dictionary<TKey, TValue> , . , (IDictionary<TKey, TValue> , , IReadOnlyDictionary<TKey, TValue> -both, Dictionary), .

, , (, , , ) - IEnumerable<T> , .

+4

Source: https://habr.com/ru/post/1626801/


All Articles