|
1 | 1 | --- |
2 | | -title: "Passing Arrays as Arguments (C# Programming Guide)" |
| 2 | +title: "Passing arrays as arguments (C# Programming Guide)" |
3 | 3 | ms.date: 07/05/2018 |
4 | 4 | helpviewer_keywords: |
5 | 5 | - "arrays [C#], passing as arguments" |
6 | 6 | ms.assetid: f3a0971e-c87c-4a1f-8262-bc0a3b712772 |
7 | 7 | --- |
8 | | -# Passing Arrays as Arguments (C# Programming Guide) |
9 | | -Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements. |
10 | | - |
11 | | -## Passing Single-Dimensional Arrays As Arguments |
12 | | - You can pass an initialized single-dimensional array to a method. For example, the following statement sends an array to a print method. |
13 | | - |
14 | | - [!code-csharp[csProgGuideArrays#34](codesnippet/CSharp/passing-arrays-as-arguments_1.cs)] |
15 | | - |
16 | | - The following code shows a partial implementation of the print method. |
17 | | - |
18 | | - [!code-csharp[csProgGuideArrays#33](codesnippet/CSharp/passing-arrays-as-arguments_2.cs)] |
19 | | - |
20 | | - You can initialize and pass a new array in one step, as is shown in the following example. |
21 | | - |
22 | | - [!code-csharp[CsProgGuideArrays#35](codesnippet/CSharp/passing-arrays-as-arguments_3.cs)] |
23 | | - |
24 | | -## Example |
25 | | - |
26 | | -### Description |
27 | | - In the following example, an array of strings is initialized and passed as an argument to a `DisplayArray` method for strings. The method displays the elements of the array. Next, the `ChangeArray` method reverses the array elements, and then the `ChangeArrayElements` method modifies the first three elements of the array. After each method returns, the `DisplayArray` method shows that passing an array by value does not prevent changes to the array elements. |
28 | | - |
29 | | -### Code |
30 | | - [!code-csharp[csProgGuideArrays#30](codesnippet/CSharp/passing-arrays-as-arguments_4.cs)] |
31 | | - |
32 | | -## Passing Multidimensional Arrays As Arguments |
33 | | - You pass an initialized multidimensional array to a method in the same way that you pass a one-dimensional array. |
34 | | - |
35 | | - [!code-csharp[csProgGuideArrays#41](codesnippet/CSharp/passing-arrays-as-arguments_5.cs)] |
36 | | - |
37 | | - The following code shows a partial declaration of a print method that accepts a two-dimensional array as its argument. |
38 | | - |
39 | | - [!code-csharp[csProgGuideArrays#36](codesnippet/CSharp/passing-arrays-as-arguments_6.cs)] |
40 | | - |
41 | | - You can initialize and pass a new array in one step, as is shown in the following example. |
42 | | - |
43 | | - [!code-csharp[csProgGuideArrays#32](codesnippet/CSharp/passing-arrays-as-arguments_7.cs)] |
44 | | - |
45 | | -## Example |
46 | | - |
47 | | -### Description |
48 | | - In the following example, a two-dimensional array of integers is initialized and passed to the `Print2DArray` method. The method displays the elements of the array. |
49 | | - |
50 | | -### Code |
51 | | - [!code-csharp[csProgGuideArrays#31](codesnippet/CSharp/passing-arrays-as-arguments_8.cs)] |
52 | | - |
53 | | -## See Also |
54 | | - [C# Programming Guide](../../../csharp/programming-guide/index.md) |
55 | | - [Arrays](index.md) |
56 | | - [Single-Dimensional Arrays](single-dimensional-arrays.md) |
57 | | - [Multidimensional Arrays](multidimensional-arrays.md) |
58 | | - [Jagged Arrays](jagged-arrays.md) |
| 8 | +# Passing arrays as arguments (C# Programming Guide) |
| 9 | + |
| 10 | +Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements. |
| 11 | + |
| 12 | +## Passing single-dimensional arrays as arguments |
| 13 | + |
| 14 | +You can pass an initialized single-dimensional array to a method. For example, the following statement sends an array to a print method. |
| 15 | + |
| 16 | +[!code-csharp[csProgGuideArrays#34](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#34)] |
| 17 | + |
| 18 | +The following code shows a partial implementation of the print method. |
| 19 | + |
| 20 | +[!code-csharp[csProgGuideArrays#33](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#33)] |
| 21 | + |
| 22 | +You can initialize and pass a new array in one step, as is shown in the following example. |
| 23 | + |
| 24 | +[!code-csharp[CsProgGuideArrays#35](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#35)] |
| 25 | + |
| 26 | +### Example |
| 27 | + |
| 28 | +In the following example, an array of strings is initialized and passed as an argument to a `DisplayArray` method for strings. The method displays the elements of the array. Next, the `ChangeArray` method reverses the array elements, and then the `ChangeArrayElements` method modifies the first three elements of the array. After each method returns, the `DisplayArray` method shows that passing an array by value doesn't prevent changes to the array elements. |
| 29 | + |
| 30 | +[!code-csharp[csProgGuideArrays#30](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/ArrayExample.cs)] |
| 31 | + |
| 32 | +## Passing multidimensional arrays as arguments |
| 33 | + |
| 34 | +You pass an initialized multidimensional array to a method in the same way that you pass a one-dimensional array. |
| 35 | + |
| 36 | +[!code-csharp[csProgGuideArrays#41](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#41)] |
| 37 | + |
| 38 | +The following code shows a partial declaration of a print method that accepts a two-dimensional array as its argument. |
| 39 | + |
| 40 | +[!code-csharp[csProgGuideArrays#36](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#36)] |
| 41 | + |
| 42 | +You can initialize and pass a new array in one step, as is shown in the following example: |
| 43 | + |
| 44 | +[!code-csharp[csProgGuideArrays#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#32)] |
| 45 | + |
| 46 | +### Example |
| 47 | + |
| 48 | +In the following example, a two-dimensional array of integers is initialized and passed to the `Print2DArray` method. The method displays the elements of the array. |
| 49 | + |
| 50 | +[!code-csharp[csProgGuideArrays#31](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideArrays/CS/Arrays.cs#31)] |
| 51 | + |
| 52 | +## See also |
| 53 | + |
| 54 | +[C# Programming Guide](../index.md) |
| 55 | +[Arrays](index.md) |
| 56 | +[Single-Dimensional Arrays](single-dimensional-arrays.md) |
| 57 | +[Multidimensional Arrays](multidimensional-arrays.md) |
| 58 | +[Jagged Arrays](jagged-arrays.md) |
0 commit comments