Don't use .NET 1.x HashTable and ArrayList
Deprecated, disabled by default; re-enable if it fits your environment. Warns about methods using non-generic collections: ArrayList, HashTable, Queue, Stack or SortedList.
How to fix
Prefer generic collections: strongly typed elements, faster with value types (no boxing):
- List<T> over ArrayList.
- Dictionary<K,V> over HashTable.
- Queue<T> over Queue.
- Stack<T> over Stack.
- SortedDictionary<K,V> or SortedList<K,V> over SortedList.
Being forced to use non-generic collections (third-party code, .NET 1.x) should question moving to newer .NET updates.
- List<T> over ArrayList.
- Dictionary<K,V> over HashTable.
- Queue<T> over Queue.
- Stack<T> over Stack.
- SortedDictionary<K,V> or SortedList<K,V> over SortedList.
Being forced to use non-generic collections (third-party code, .NET 1.x) should question moving to newer .NET updates.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Don't use .NET 1.x HashTable and ArrayList</Name>
// <Id>ND2301:DontUseDotNET1HashTableAndArrayList</Id>
warnif count > 0
let forbiddenTypes = ThirdParty.Types.WithFullNameIn(
"System.Collections.HashTable",
"System.Collections.ArrayList",
"System.Collections.Queue",
"System.Collections.Stack",
"System.Collections.SortedList")
where forbiddenTypes.Count() > 0
from m in Application.Methods.ThatCreateAny(forbiddenTypes)
let forbiddenTypesUsed = m.MethodsCalled.Where(m1 => m1.IsConstructor && forbiddenTypes.Contains(m1.ParentType)).ParentTypes()
select new {
m,
forbiddenTypesUsed,
Debt = 15.ToMinutes().ToDebt(),
Severity = Severity.Medium
}
//<Expl>
// The *{0}* shouldn't use the *{1}* because there {1?is a:are}
// better generic alternative{1?:s} in the .NET Base Class Library.
//</Expl>
//<Desc>
// Deprecated, disabled by default; re-enable if it fits your environment.
// Warns about methods using non-generic collections: **ArrayList**,
// **HashTable**, **Queue**, **Stack** or **SortedList**.
//</Desc>
//<HowToFix>
// Prefer generic collections: strongly typed elements, faster with value
// types (no boxing):
//
// - **List<T>** over **ArrayList**.
//
// - **Dictionary<K,V>** over **HashTable**.
//
// - **Queue<T>** over **Queue**.
//
// - **Stack<T>** over **Stack**.
//
// - **SortedDictionary<K,V>** or **SortedList<K,V>** over **SortedList**.
//
// Being forced to use non-generic collections (third-party code, .NET 1.x)
// should question moving to newer .NET updates.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
.NET Framework Usage / System.Collections: all 4 items
- Collection properties should be read only ND2300
- Don't use .NET 1.x HashTable and ArrayList ND2301
- Caution with List.Contains() ND2302
- Prefer return collection abstraction instead of implementation ND2303