Cause
An empty System.Array with no elements is allocated.
Rule description
Initializing a zero-length array leads to an unnecessary memory allocation. Instead, use the statically allocated empty array instance by calling the System.Array.Empty method. The memory allocation is shared across all invocations of this method.
How to fix violations
To fix a violation, replace the zero-length array allocation with a call to System.Array.Empty. For example, the following two code snippets show a violation of the rule and how to fix it:
class C
{
public void M1()
{
// Violates rule CA1825.
var a = new int[0];
}
}class C
{
public void M1()
{
// Resolves rule CA1825 violation.
var a = System.Array.Empty<int>();
}
}When to suppress warnings
It's safe to suppress a violation of this rule if you're not concerned about the additional memory allocation.
Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1825
// The code that's violating the rule is on this line.
#pragma warning restore CA1825To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1825.severity = noneFor more information, see How to suppress code analysis warnings.