Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Prog_Guide.doc
Скачиваний:
16
Добавлен:
16.11.2019
Размер:
6.22 Mб
Скачать

Пример 1 Описание

В следующем примере массив строк инициализируется и передается в качестве параметра в метод PrintArray, где отображаются его элементы:

Код

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 Main()

{

// Declare and initialize an array:

string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

// Pass the array as a parameter:

PrintArray(weekDays);

}

}

Результат 1

Sun Mon Tue Wed Thu Fri Sat

Passing multidimensional arrays as parameters

You can pass an initialized multidimensional array to a method. For example, if theArray is a two dimensional array:

PrintArray(theArray);

The method called in the line above could be defined as:

void PrintArray(int[,] arr)

{

// method code

}

You can also initialize and pass a new array in one step. For example:

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

Передача многомерных массивов в качестве параметров

Инициализированный многомерный массив можно передать в метод. Например, если theArray является двумерным массивом:

PrintArray(theArray);

Метод, вызываемый в приведенной выше строке, может быть задан как:

void PrintArray(int[,] arr)

{

// method code

}

Новый массив можно инициализировать и передать за одно действие. Пример.

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

Example 2

Description

In this example, a two-dimensional array is initialized and passed to the PrintArray method, where its elements are displayed.

Code

class ArrayClass2D

{

static void PrintArray(int[,] arr)

{

// Display the array elements:

for (int i = 0; i < 4; i++)

{

for (int j = 0; j < 2; j++)

{

System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);

}

}

}

static void Main()

{

// Pass the array as a parameter:

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

}

}

Output 2

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

Пример 2 Описание

В данном примере двумерный массив строк инициализируется и передается в метод PrintArray, где отображаются его элементы.

Код

--

Passing Arrays Using ref and out

Like all out parameters, an out parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee. For example:

static void TestMethod1(out int[] arr)

{

arr = new int[10]; // definite assignment of arr

}

Like all ref parameters, a ref parameter of an array type must be definitely assigned by the caller. Therefore, there is no need to be definitely assigned by the callee. A ref parameter of an array type may be altered as a result of the call. For example, the array can be assigned the null value or can be initialized to a different array. For example:

static void TestMethod2(ref int[] arr)

{

arr = new int[10]; // arr initialized to a different array

}

The following two examples demonstrate the difference between out and ref when used in passing arrays to methods.