logo
CSharp_Prog_Guide

Пример 2

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

--

Implicitly Typed Arrays

You can create an implicitly-typed array in which the type of the array instance is inferred from the elements specified in the array initializer. The rules for any implicitly-typed variable also apply to implicitly-typed arrays.

Implicitly-typed arrays are usually used in query expressions together with anonymous types and object and collection initializers.

The following examples show how to create an implicitly-typed array:

class ImplicitlyTypedArraySample

{

static void Main()

{

var a = new[] { 1, 10, 100, 1000 }; // int[]

var b = new[] { "hello", null, "world" }; // string[]

// single-dimension jagged array

var c = new[]

{

new[]{1,2,3,4},

new[]{5,6,7,8}

};

// jagged array of strings

var d = new[]

{

new[]{"Luca", "Mads", "Luke", "Dinesh"},

new[]{"Karen", "Suma", "Frances"}

};

}

}

In the previous example, notice that with implicitly-typed arrays, no square brackets are used on the left side of the initialization statement. Note also that jagged arrays are initialized by using new [] just like single-dimension arrays. Multidimensional implicitly-typed arrays are not supported.