platform-compat/docs/DE0006.md

126 строки
9.7 KiB
Markdown
Исходник Постоянная ссылка Обычный вид История

<!--
M:System.Collections.ArrayList.Repeat(System.Object,System.Int32)
M:System.Collections.ArrayList.ReadOnly(System.Collections.ArrayList)
M:System.Collections.ArrayList.Synchronized(System.Collections.ArrayList)
M:System.Collections.ArrayList.#ctor(System.Collections.ICollection)
M:System.Collections.ArrayList.Adapter(System.Collections.IList)
M:System.Collections.ArrayList.#ctor
M:System.Collections.ArrayList.#ctor(System.Int32)
M:System.Collections.ArrayList.FixedSize(System.Collections.ArrayList)
M:System.Collections.Hashtable.#ctor(System.Int32,System.Single,System.Collections.IHashCodeProvider,System.Collections.IComparer)
M:System.Collections.Hashtable.#ctor(System.Collections.IDictionary,System.Collections.IEqualityComparer)
M:System.Collections.Hashtable.#ctor(System.Collections.IDictionary,System.Single,System.Collections.IEqualityComparer)
M:System.Collections.Hashtable.#ctor(System.Int32,System.Single,System.Collections.IEqualityComparer)
M:System.Collections.Hashtable.#ctor(System.Collections.IHashCodeProvider,System.Collections.IComparer)
M:System.Collections.Hashtable.#ctor(System.Int32,System.Collections.IHashCodeProvider,System.Collections.IComparer)
M:System.Collections.Hashtable.Synchronized(System.Collections.Hashtable)
M:System.Collections.Hashtable.#ctor(System.Collections.IDictionary)
M:System.Collections.Hashtable.#ctor
M:System.Collections.Hashtable.#ctor(System.Int32,System.Collections.IEqualityComparer)
M:System.Collections.Hashtable.#ctor(System.Collections.IDictionary,System.Collections.IHashCodeProvider,System.Collections.IComparer)
M:System.Collections.Hashtable.#ctor(System.Collections.IDictionary,System.Single,System.Collections.IHashCodeProvider,System.Collections.IComparer)
M:System.Collections.Hashtable.#ctor(System.Int32,System.Single)
M:System.Collections.Hashtable.#ctor(System.Collections.IEqualityComparer)
M:System.Collections.Hashtable.#ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)
M:System.Collections.Hashtable.#ctor(System.Collections.IDictionary,System.Single)
M:System.Collections.Hashtable.#ctor(System.Int32)
M:System.Collections.Queue.#ctor(System.Int32,System.Single)
M:System.Collections.Queue.#ctor
M:System.Collections.Queue.Synchronized(System.Collections.Queue)
M:System.Collections.Queue.#ctor(System.Int32)
M:System.Collections.Queue.#ctor(System.Collections.ICollection)
M:System.Collections.Stack.#ctor(System.Int32)
M:System.Collections.Stack.Synchronized(System.Collections.Stack)
M:System.Collections.Stack.#ctor(System.Collections.ICollection)
M:System.Collections.Stack.#ctor
M:System.Collections.SortedList.#ctor(System.Int32)
M:System.Collections.SortedList.#ctor(System.Collections.IComparer)
M:System.Collections.SortedList.#ctor(System.Collections.IComparer,System.Int32)
M:System.Collections.SortedList.Synchronized(System.Collections.SortedList)
M:System.Collections.SortedList.#ctor(System.Collections.IDictionary,System.Collections.IComparer)
M:System.Collections.SortedList.#ctor
M:System.Collections.SortedList.#ctor(System.Collections.IDictionary)
M:System.Collections.DictionaryEntry.#ctor(System.Object,System.Object)
T:System.Collections.DictionaryBase
T:System.Collections.CollectionBase
T:System.Collections.ReadOnlyCollectionBase
M:System.Collections.Comparer.#ctor(System.Globalization.CultureInfo)
M:System.Collections.CaseInsensitiveComparer.get_Default
M:System.Collections.CaseInsensitiveComparer.get_DefaultInvariant
M:System.Collections.CaseInsensitiveComparer.#ctor
M:System.Collections.CaseInsensitiveComparer.#ctor(System.Globalization.CultureInfo)
M:System.Collections.CaseInsensitiveHashCodeProvider.#ctor(System.Globalization.CultureInfo)
M:System.Collections.CaseInsensitiveHashCodeProvider.#ctor
M:System.Collections.CaseInsensitiveHashCodeProvider.get_Default
M:System.Collections.CaseInsensitiveHashCodeProvider.get_DefaultInvariant
-->
# DE0006: Non-generic collections shouldn't be used
## Motivation
When .NET was created, generic data types didn't exist, which is why the
collection types in the [`System.Collections`][collections] namespace are untyped. However, since then,
generic data types were introduced and thus a new set of collections
were made available in the [`System.Collections.Generic`][generic] and
[`System.Collections.ObjectModel`][objectmodel] namespaces.
## Recommendation
For new code, you shouldn't use non-generic collections:
* **Error prone**: since non-generic collections are untyped, it requires frequent
casting between `object` and the actual type you're expecting. Since the compiler
can't check that your types are consistent, it's easier to put the wrong type in
the wrong collection.
* **Less performant**: generic collections have the advantage that value types
don't have to be boxed as object. For instance, a `List<int>` stores its data
in an `int[]`. That's far better than storing the data in `object[]` as that
requires boxing.
The following table shows how the non-generic collection types can be
replaced by their generic counterparts from the [`System.Collections.Generic`][generic] or
[`System.Collections.ObjectModel`][objectmodel] namespaces:
| Type | Replacement |
|----------------------------------------------------------------------|-----------------------------------------------------------------------------------------------|
| [`ArrayList`][arraylist] | [`List<T>`][list] |
| [`CaseInsensitiveComparer`][caseinsensitivecomparer] | [`StringComparer.OrdinalIgnoreCase`][ordinalignorecase] |
| [`CaseInsensitiveHashCodeProvider`][caseinsensitivehashcodeprovider] | [`StringComparer.OrdinalIgnoreCase`][ordinalignorecase] |
| [`CollectionBase`][collectionbase] | [`Collection<T>`][collection-1] |
| [`Comparer`][comparer] | [`Comparer<T>`][comparer-1] |
| [`DictionaryBase`][dictionarybase] | [`Dictionary<TKey, TValue>`][dictionary] or [`KeyedCollection<TKey,TItem>`][keyedcollection] |
| [`DictionaryEntry`][dictionaryentry] | [`KeyValuePair<TKey, TValue>`][keyvaluepair] |
| [`Hashtable`][hashtable] | [`Dictionary<TKey, TValue>`][dictionary] |
| [`Queue`][queue] | [`Queue<T>`][queue-1] |
| [`ReadOnlyCollectionBase`][readonlycollectionbase] | [`ReadOnlyCollection<T>`][readonlycollection] |
| [`SortedList`][sortedlist] | [`SortedList<TKey,TValue>`][sortedlist-2] |
| [`Stack`][stack] | [`Stack<T>`][stack-1] |
[arraylist]: https://docs.microsoft.com/dotnet/api/system.collections.arraylist
[caseinsensitivecomparer]: https://docs.microsoft.com/dotnet/api/system.collections.caseinsensitivecomparer
[caseinsensitivehashcodeprovider]: https://docs.microsoft.com/dotnet/api/system.collections.caseinsensitivehashcodeprovider
[collection-1]: https://docs.microsoft.com/dotnet/api/system.collections.objectmodel.collection-1
[collectionbase]: https://docs.microsoft.com/dotnet/api/system.collections.collectionbase
[collections]: https://docs.microsoft.com/dotnet/api/system.collections
[comparer]: https://docs.microsoft.com/dotnet/api/system.collections.comparer
[comparer-1]: https://docs.microsoft.com/dotnet/api/system.collections.generic.comparer-1
[dictionary]: https://docs.microsoft.com/dotnet/api/system.collections.generic.dictionary-2
[dictionarybase]: https://docs.microsoft.com/dotnet/api/system.collections.dictionarybase
[dictionaryentry]: https://docs.microsoft.com/dotnet/api/system.collections.dictionaryentry
[generic]: https://docs.microsoft.com/dotnet/api/system.collections.generic
[hashtable]: https://docs.microsoft.com/dotnet/api/system.collections.hashtable
[keyedcollection]: https://docs.microsoft.com/dotnet/api/system.collections.objectmodel.keyedcollection-2
[keyvaluepair]: https://docs.microsoft.com/dotnet/api/system.collections.generic.keyvaluepair-2
[list]: https://docs.microsoft.com/dotnet/api/system.collections.generic.list-1
[objectmodel]: https://docs.microsoft.com/dotnet/api/system.collections.objectmodel
[ordinalignorecase]: https://docs.microsoft.com/dotnet/api/system.stringcomparer.ordinalignorecase
[queue]: https://docs.microsoft.com/dotnet/api/system.collections.queue
[queue-1]: https://docs.microsoft.com/dotnet/api/system.collections.generic.queue-1
[readonlycollection]: https://docs.microsoft.com/dotnet/api/system.collections.objectmodel.readonlycollection-1
[readonlycollectionbase]: https://docs.microsoft.com/dotnet/api/system.collections.readonlycollectionbase
[stack]: https://docs.microsoft.com/dotnet/api/system.collections.stack
[stack-1]: https://docs.microsoft.com/dotnet/api/system.collections.generic.stack-1
[sortedlist]: https://docs.microsoft.com/dotnet/api/system.collections.sortedlist
[sortedlist-2]: https://docs.microsoft.com/dotnet/api/system.collections.generic.sortedlist-2