logo
CSharp_Prog_Guide

Пример 1

В этом примере массив theArray объявлен в вызывающем методе (метод Main) и инициализирован внутри вызванного метода FillArray. Затем элементы инициализированного таким образом массива отображаются в вызывающем методе Main.

--

Example 2

In this example, the array theArray is initialized in the caller (the Main method), and passed to the FillArray method by using the ref parameter. Some of the array elements are updated in the FillArray method. Then, the array elements are returned to the caller and displayed.

class TestRef

{

static void FillArray(ref int[] arr)

{

// Create the array on demand:

if (arr == null)

{

arr = new int[10];

}

// Fill the array:

arr[0] = 1111;

arr[4] = 5555;

}

static void Main()

{

// Initialize the array:

int[] theArray = { 1, 2, 3, 4, 5 };

// Pass the array using ref:

FillArray(ref theArray);

// Display the updated array:

System.Console.WriteLine("Array elements are:");

for (int i = 0; i < theArray.Length; i++)

{

System.Console.Write(theArray[i] + " ");

}

}

}

Output 2

Array elements are:

1111 2 3 4 5555