将数组作为参数传递(C# 编程指南)

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

将一维数组作为参数传递

可以将初始化的一维数组传递给方法。例如,下面的语句将数组发送到 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 });

示例

hyfeyz71.collapse_all(zh-cn,VS.110).gif说明

在下面的示例中,将初始化一个字符串数组并将其作为参数传递到字符串的 PrintArray 方法。该方法显示数组的元素。接下来,调用 ChangeArray 和 ChangeArrayElement 方法以演示通过值发送数组参数时不会阻止更改这些数组元素。

hyfeyz71.collapse_all(zh-cn,VS.110).gif代码

class ArrayClass
{
    static void PrintArray(string[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
        }
        System.Console.WriteLine();
    }

    static void ChangeArray(string[] arr)
    {
        // The following attempt to reverse the array does not persist when
        // the method returns, because arr is a value parameter.
        arr = (arr.Reverse()).ToArray();
        // The following statement displays Sat as the first element in the array.
        System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
    }

    static void ChangeArrayElements(string[] arr)
    {
        // The following assignments change the value of individual array 
        // elements. 
        arr[0] = "Sat";
        arr[1] = "Fri";
        arr[2] = "Thu";
        // The following statement again displays Sat as the first element
        // in the array arr, inside the called method.
        System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]);
    }

    static void Main()
    {
        // Declare and initialize an array.
        string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

        // Pass the array as an argument to PrintArray.
        PrintArray(weekDays);

        // ChangeArray tries to change the array by assigning something new
        // to the array in the method. 
        ChangeArray(weekDays);

        // Print the array again, to verify that it has not been changed.
        System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
        PrintArray(weekDays);
        System.Console.WriteLine();

        // ChangeArrayElements assigns new values to individual array
        // elements.
        ChangeArrayElements(weekDays);

        // The changes to individual elements persist after the method returns.
        // Print the array, to verify that it has been changed.
        System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:");
        PrintArray(weekDays);
    }
}
// Output: 
// Sun Mon Tue Wed Thu Fri Sat
// arr[0] is Sat in ChangeArray.
// Array weekDays after the call to ChangeArray:
// Sun Mon Tue Wed Thu Fri Sat
// 
// arr[0] is Sat in ChangeArrayElements.
// Array weekDays after the call to ChangeArrayElements:
// Sat Fri Thu Wed Thu Fri Sat

将多维数组作为参数传递

可采用与传递一维数组相同的方式将初始化的多维数组传递给方法。

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

示例

hyfeyz71.collapse_all(zh-cn,VS.110).gif说明

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

hyfeyz71.collapse_all(zh-cn,VS.110).gif代码

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# 编程指南)

一维数组(C# 编程指南)

多维数组(C# 编程指南)

交错数组(C# 编程指南)

概念

C# 编程指南