logo
CSharp_Prog_Guide

Пример 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.