Skip to content

Commit fd1beae

Browse files
authored
update ca1814 description (#22088)
1 parent eab4a32 commit fd1beae

File tree

4 files changed

+97
-89
lines changed

4 files changed

+97
-89
lines changed

docs/csharp/programming-guide/arrays/jagged-arrays.md

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
11
---
2-
title: "Jagged Arrays - C# Programming Guide"
3-
description: A jagged array in C# is an array whose elements are arrays of different dimensions and sizes. Learn how to declare, initialize, and access jagged arrays.
4-
ms.date: 07/20/2015
5-
helpviewer_keywords:
2+
title: Jagged Arrays - C# Programming Guide
3+
description: A jagged array in C# is an array whose elements are arrays of different sizes. Learn how to declare, initialize, and access jagged arrays.
4+
ms.date: 12/18/2020
5+
helpviewer_keywords:
66
- "jagged arrays [C#]"
77
- "arrays [C#], jagged"
88
ms.assetid: 537c65a6-0e0a-4a00-a2b8-086f38519c70
99
---
1010
# Jagged Arrays (C# Programming Guide)
1111

12-
A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.
13-
14-
The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:
15-
16-
[!code-csharp[csProgGuideArrays#19](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#19)]
17-
18-
Before you can use `jaggedArray`, its elements must be initialized. You can initialize the elements like this:
19-
20-
[!code-csharp[csProgGuideArrays#20](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#20)]
21-
22-
Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers.
23-
24-
It is also possible to use initializers to fill the array elements with values, in which case you do not need the array size. For example:
25-
26-
[!code-csharp[csProgGuideArrays#21](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#21)]
27-
28-
You can also initialize the array upon declaration like this:
29-
30-
[!code-csharp[csProgGuideArrays#22](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#22)]
31-
32-
You can use the following shorthand form. Notice that you cannot omit the `new` operator from the elements initialization because there is no default initialization for the elements:
33-
34-
[!code-csharp[csProgGuideArrays#23](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#23)]
35-
36-
A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to `null`.
37-
38-
You can access individual array elements like these examples:
39-
40-
[!code-csharp[csProgGuideArrays#24](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#24)]
41-
42-
It is possible to mix jagged and multidimensional arrays. The following is a declaration and initialization of a single-dimensional jagged array that contains three two-dimensional array elements of different sizes. For more information about two-dimensional arrays, see [Multidimensional Arrays](./multidimensional-arrays.md).
43-
44-
[!code-csharp[csProgGuideArrays#25](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#25)]
45-
46-
You can access individual elements as shown in this example, which displays the value of the element `[1,0]` of the first array (value `5`):
47-
48-
[!code-csharp[csProgGuideArrays#26](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#26)]
49-
50-
The method `Length` returns the number of arrays contained in the jagged array. For example, assuming you have declared the previous array, this line:
51-
52-
[!code-csharp[csProgGuideArrays#27](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#27)]
53-
54-
returns a value of 3.
55-
12+
A jagged array is an array whose elements are arrays, possibly of different sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.
13+
14+
The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers:
15+
16+
[!code-csharp[csProgGuideArrays#19](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#19)]
17+
18+
Before you can use `jaggedArray`, its elements must be initialized. You can initialize the elements like this:
19+
20+
[!code-csharp[csProgGuideArrays#20](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#20)]
21+
22+
Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers.
23+
24+
It is also possible to use initializers to fill the array elements with values, in which case you do not need the array size. For example:
25+
26+
[!code-csharp[csProgGuideArrays#21](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#21)]
27+
28+
You can also initialize the array upon declaration like this:
29+
30+
[!code-csharp[csProgGuideArrays#22](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#22)]
31+
32+
You can use the following shorthand form. Notice that you cannot omit the `new` operator from the elements initialization because there is no default initialization for the elements:
33+
34+
[!code-csharp[csProgGuideArrays#23](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#23)]
35+
36+
A jagged array is an array of arrays, and therefore its elements are reference types and are initialized to `null`.
37+
38+
You can access individual array elements like these examples:
39+
40+
[!code-csharp[csProgGuideArrays#24](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#24)]
41+
42+
It's possible to mix jagged and multidimensional arrays. The following is a declaration and initialization of a single-dimensional jagged array that contains three two-dimensional array elements of different sizes. For more information, see [Multidimensional Arrays](./multidimensional-arrays.md).
43+
44+
[!code-csharp[csProgGuideArrays#25](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#25)]
45+
46+
You can access individual elements as shown in this example, which displays the value of the element `[1,0]` of the first array (value `5`):
47+
48+
[!code-csharp[csProgGuideArrays#26](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#26)]
49+
50+
The method `Length` returns the number of arrays contained in the jagged array. For example, assuming you have declared the previous array, this line:
51+
52+
[!code-csharp[csProgGuideArrays#27](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#27)]
53+
54+
returns a value of 3.
55+
5656
## Example
5757

58-
This example builds an array whose elements are themselves arrays. Each one of the array elements has a different size.
59-
60-
[!code-csharp[csProgGuideArrays#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#18)]
61-
58+
This example builds an array whose elements are themselves arrays. Each one of the array elements has a different size.
59+
60+
[!code-csharp[csProgGuideArrays#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#18)]
61+
6262
## See also
6363

6464
- <xref:System.Array>

docs/fundamentals/code-analysis/quality-rules/ca1814.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "CA1814: Prefer jagged arrays over multidimensional (code analysis)"
33
description: "Learn about code analysis rule CA1814: Prefer jagged arrays over multidimensional"
4-
ms.date: 11/04/2016
4+
ms.date: 12/18/2020
55
ms.topic: reference
66
f1_keywords:
77
- PreferJaggedArraysOverMultidimensional
@@ -25,19 +25,19 @@ dev_langs:
2525

2626
## Cause
2727

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

3030
## Rule description
3131

32-
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.
32+
In a [multidimensional array](../../../csharp/programming-guide/arrays/multidimensional-arrays.md), each element in each dimension has the same, fixed size as the other elements in that dimension. In a [jagged array](../../../csharp/programming-guide/arrays/jagged-arrays.md), 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.
3333

3434
## How to fix violations
3535

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

3838
## When to suppress warnings
3939

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

4242
## Example
4343

samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ static void Main()
3232
System.Console.ReadKey();
3333
}
3434
}
35-
/* Output:
36-
Array elements are:
37-
1111 2 3 4 5555
38-
*/
35+
/* Output:
36+
Array elements are:
37+
1111 2 3 4 5555
38+
*/
3939
//</Snippet38>
4040

4141
//<Snippet37>
@@ -66,10 +66,10 @@ static void Main()
6666
System.Console.ReadKey();
6767
}
6868
}
69-
/* Output:
70-
Array elements are:
71-
1 2 3 4 5
72-
*/
69+
/* Output:
70+
Array elements are:
71+
1 2 3 4 5
72+
*/
7373
//</Snippet37>
7474

7575
class TestPrintArray1D
@@ -98,7 +98,7 @@ void Test()
9898

9999
class TestPrintArray2D
100100
{
101-
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
101+
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
102102

103103
//<Snippet36>
104104
void Print2DArray(int[,] arr)
@@ -142,16 +142,16 @@ static void Main()
142142
System.Console.ReadKey();
143143
}
144144
}
145-
/* Output:
146-
Element(0,0)=1
147-
Element(0,1)=2
148-
Element(1,0)=3
149-
Element(1,1)=4
150-
Element(2,0)=5
151-
Element(2,1)=6
152-
Element(3,0)=7
153-
Element(3,1)=8
154-
*/
145+
/* Output:
146+
Element(0,0)=1
147+
Element(0,1)=2
148+
Element(1,0)=3
149+
Element(1,1)=4
150+
Element(2,0)=5
151+
Element(2,1)=6
152+
Element(3,0)=7
153+
Element(3,1)=8
154+
*/
155155
//</Snippet31>
156156

157157
class Test3
@@ -165,7 +165,7 @@ static void Main()
165165
System.Console.Write("{0} ", i);
166166
}
167167
// Output: 4 5 6 1 2 3 -2 -1 0
168-
//</Snippet28>
168+
//</Snippet28>
169169

170170
System.Console.WriteLine();
171171

@@ -221,7 +221,7 @@ void test()
221221
//</Snippet11>
222222

223223
//<Snippet12>
224-
int[, ,] array1 = new int[4, 2, 3];
224+
int[,,] array1 = new int[4, 2, 3];
225225
//</Snippet12>
226226

227227
//<Snippet13>
@@ -234,10 +234,10 @@ void test()
234234
{ "five", "six" } };
235235

236236
// Three-dimensional array.
237-
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
237+
int[,,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
238238
{ { 7, 8, 9 }, { 10, 11, 12 } } };
239239
// The same array with dimensions specified.
240-
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
240+
int[,,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
241241
{ { 7, 8, 9 }, { 10, 11, 12 } } };
242242

243243
// Accessing array elements.
@@ -253,7 +253,8 @@ void test()
253253
// Getting the total count of elements or the length of a given dimension.
254254
var allLength = array3D.Length;
255255
var total = 1;
256-
for (int i = 0; i < array3D.Rank; i++) {
256+
for (int i = 0; i < array3D.Rank; i++)
257+
{
257258
total *= array3D.GetLength(i);
258259
}
259260
System.Console.WriteLine("{0} equals {1}", allLength, total);
@@ -395,21 +396,21 @@ void test()
395396
//</Snippet21>
396397

397398
//<Snippet22>
398-
int[][] jaggedArray2 = new int[][]
399-
{
399+
int[][] jaggedArray2 = new int[][]
400+
{
400401
new int[] { 1, 3, 5, 7, 9 },
401402
new int[] { 0, 2, 4, 6 },
402403
new int[] { 11, 22 }
403-
};
404+
};
404405
//</Snippet22>
405406

406407
//<Snippet23>
407-
int[][] jaggedArray3 =
408-
{
409-
new int[] { 1, 3, 5, 7, 9 },
410-
new int[] { 0, 2, 4, 6 },
411-
new int[] { 11, 22 }
412-
};
408+
int[][] jaggedArray3 =
409+
{
410+
new int[] { 1, 3, 5, 7, 9 },
411+
new int[] { 0, 2, 4, 6 },
412+
new int[] { 11, 22 }
413+
};
413414
//</Snippet23>
414415

415416
//<Snippet24>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

0 commit comments

Comments
 (0)