Export (0) Print
Expand All

Prefer jagged arrays over multidimensional

TypeName

PreferJaggedArraysOverMultidimensional

CheckId

CA1814

Category

Microsoft.Performance

Breaking Change

Breaking

A member is declared as a multidimensional array.

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.

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

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

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}
                                };
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft