将数组作为参数传递 您所在的位置:网站首页 如何定义字符数组并赋值 将数组作为参数传递

将数组作为参数传递

2023-07-10 19:30| 来源: 网络整理| 查看: 265

将数组作为参数传递(C# 编程指南) 项目 04/07/2023

数组可以作为实参传递给方法形参。 由于数组是引用类型,因此方法可以更改元素的值。

将一维数组作为参数传递

可将初始化的一维数组传递给方法。 例如,下列语句将一个数组发送给了 Print 方法。

int[] theArray = { 1, 3, 5, 7, 9 }; PrintArray(theArray);

下面的代码演示 Print 方法的部分实现。

void PrintArray(int[] arr) { // Method code. }

可在同一步骤中初始化并传递新数组,如下例所示。

PrintArray(new int[] { 1, 3, 5, 7, 9 }); 示例

在下面的示例中,先初始化一个字符串数组,然后将其作为参数传递给字符串的 DisplayArray 方法。 该方法将显示数组的元素。 接下来,ChangeArray 方法会反转数组元素,然后由 ChangeArrayElements 方法修改该数组的前三个元素。 每个方法返回后,DisplayArray 方法会显示按值传递数组不会阻止对数组元素的更改。

using System; class ArrayExample { static void DisplayArray(string[] arr) => Console.WriteLine(string.Join(" ", arr)); // Change the array by reversing its elements. static void ChangeArray(string[] arr) => Array.Reverse(arr); static void ChangeArrayElements(string[] arr) { // Change the value of the first three array elements. arr[0] = "Mon"; arr[1] = "Wed"; arr[2] = "Fri"; } static void Main() { // Declare and initialize an array. string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; // Display the array elements. DisplayArray(weekDays); Console.WriteLine(); // Reverse the array. ChangeArray(weekDays); // Display the array again to verify that it stays reversed. Console.WriteLine("Array weekDays after the call to ChangeArray:"); DisplayArray(weekDays); Console.WriteLine(); // Assign new values to individual array elements. ChangeArrayElements(weekDays); // Display the array again to verify that it has changed. Console.WriteLine("Array weekDays after the call to ChangeArrayElements:"); DisplayArray(weekDays); } } // The example displays the following output: // Sun Mon Tue Wed Thu Fri Sat // // Array weekDays after the call to ChangeArray: // Sat Fri Thu Wed Tue Mon Sun // // Array weekDays after the call to ChangeArrayElements: // Mon Wed Fri Wed Tue Mon Sun 将多维数组作为参数传递

通过与传递一维数组相同的方式,向方法传递初始化的多维数组。

int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } }; Print2DArray(theArray);

下列代码演示了 Print 方法的部分声明(该方法接受将二维数组作为其参数)。

void Print2DArray(int[,] arr) { // Method code. }

可在一个步骤中初始化并传递新数组,如下例所示:

Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }); 示例

在下列示例中,初始化一个整数的二维数组,并将其传递至 Print2DArray 方法。 该方法将显示数组的元素。

class ArrayClass2D { static void Print2DArray(int[,] arr) { // Display the array elements. for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]); } } } static void Main() { // Pass the array as an argument. Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: Element(0,0)=1 Element(0,1)=2 Element(1,0)=3 Element(1,1)=4 Element(2,0)=5 Element(2,1)=6 Element(3,0)=7 Element(3,1)=8 */ 请参阅 C# 编程指南 数组 一维数组 多维数组 交错数组


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有