Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 2.69 KB

File metadata and controls

68 lines (49 loc) · 2.69 KB
title description ms.date ms.topic f1_keywords helpviewer_keywords author ms.author dev_langs
CA1814: Prefer jagged arrays over multidimensional (code analysis)
Learn about code analysis rule CA1814: Prefer jagged arrays over multidimensional
12/18/2020
reference
PreferJaggedArraysOverMultidimensional
CA1814
PreferJaggedArraysOverMultidimensional
CA1814
gewarren
gewarren
CSharp
VB

CA1814: Prefer jagged arrays over multidimensional

Value
Rule ID CA1814
Category Performance
Fix is breaking or non-breaking Breaking
Enabled by default in .NET 7 No

Cause

A member is declared as a multidimensional array, which can result in wasted space for some data sets.

Rule description

In a multidimensional array, each element in each dimension has the same, fixed size as the other elements in that dimension. In a jagged array, which is an array of arrays, each inner array can be of a different size. By only using the space that's needed for a given array, no space is wasted. This rule, CA1814, recommends switching to a jagged array to conserve memory.

How to fix violations

To fix a violation of this rule, change the multidimensional array to a jagged array.

When to suppress warnings

It's okay to suppress a warning from this rule if the multidimensional array does not waste space.

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 CA1814
// The code that's violating the rule is on this line.
#pragma warning restore CA1814

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1814.severity = none

For more information, see How to suppress code analysis warnings.

Example

The following example shows declarations for jagged and multidimensional arrays.

:::code language="vb" source="snippets/vb/all-rules/ca1814-prefer-jagged-arrays-over-multidimensional_1.vb":::

:::code language="csharp" source="snippets/csharp/all-rules/ca1814.cs" id="snippet1":::