| Property | Value |
|---|---|
| Package | Philips.CodeAnalysis.MaintainabilityAnalyzers |
| Diagnostic ID | PH2116 |
| Category | Maintainability |
| Analyzer | AvoidArrayListAnalyzer |
| CodeFix | Available |
| Severity | Error |
| Enabled By Default | Yes |
The ArrayList should be avoided. It doesn't use generics and therefore suffers from performance issues when used with structs. Structs need to be boxed into a class on every call to ArrayList, which impacts the performance negatively.
Microsoft discourages the use of ArrayList for this reason, as can be read on MS Learn.
Microsoft provides an alternative in the List<> class, which is supporting generics and doesn't suffer from the boxing issue. Otherwise List<> is equivalent in usage to ArrayList. In this sense, it can be regarded as a plug-in replacement.
The CodeFixer does the replacement of ArrayList with List<> for you. The generic type cannot be deduced however so the CodeFixer triggers a rename dialog for the user to specify this generic type.
Code that triggers a diagnostic:
using System.Collections;
class BadExample
{
private ArrayList _list = new ArrayList();
public void Add(int item)
{
_list.Add(item);
}
}And the replacement code:
using System.Collections.Generic;
class GoodExample
{
private List<int> _list = new List<int>();
public void Add(int item)
{
_list.Add(item);
}
}This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.