Share via


CA1814: Prefer jagged arrays over multidimensional

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName PreferJaggedArraysOverMultidimensional
CheckId CA1814
Category Microsoft.Performance
Breaking Change Breaking

Cause

A member is declared as a multidimensional array.

Rule Description

A jagged array is an array whose elements are arrays. The arrays that make up the elements can be of different sizes, leading to less wasted space for some sets of data.

How to Fix Violations

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

When to Suppress Warnings

Suppress a warning from this rule if the multidimensional array does not waste space.

Example

The following example shows declarations for jagged and multidimensional arrays.

using System;

namespace PerformanceLibrary
{
    public class ArrayHolder
    {
        int[][] jaggedArray = { new int[] {1,2,3,4},
                                new int[] {5,6,7},
                                new int[] {8},
                                new int[] {9}
                              };
                              
        int [,] multiDimArray = {{1,2,3,4},
                                 {5,6,7,0},
                                 {8,0,0,0},
                                 {9,0,0,0}
                                };
    }
}
Imports System

Public Class ArrayHolder
    Private jaggedArray As Integer()() =  {New Integer() {1, 2, 3, 4}, _
                                           New Integer() {5, 6, 7}, _
                                           New Integer() {8}, _
                                           New Integer() {9}}
    
    Private multiDimArray As Integer(,) =  {{1, 2, 3, 4}, _
                                            {5, 6, 7, 0}, _
                                            {8, 0, 0, 0}, _
                                            {9, 0, 0, 0}}
End Class