Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lektsia_8VP

.pdf
Скачиваний:
7
Добавлен:
27.05.2015
Размер:
485.59 Кб
Скачать

Коллекции

Одномерный массив

Задаётся как: type[] arrayName; int[] array2 = {1, 3, 5, 7, 9};

string[] days = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};

2

Многомерный массив

int[,] array2D = new int[2,3];

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

3

«Изрезанный» (jagged) массив

int[][] jaggedArray = new int[3][]; jaggedArray[0] = new int[5]; jaggedArray[1] = new int[4]; jaggedArray[2] = new int[2];

4

Передача массивов в функции

static void TestMethod1(out int[] arr)

{

arr = new int[10];

}

static void TestMethod2(ref int[] arr)

{

arr = new int[10];

}

5

Отличие out от ref

static void Main()

{

int x; foo(out x);

}

static void foo(out int x)

{

x = 1;

}

ref – выдаст ошибку out – будет работать

6

Пространства имён коллекций

System.Collections.Generic

System.Collections.Concurrent

System.Collections

7

Словарь (Dictionary)

Dictionary<TKey, TValue>

Tkey – ключ

Tvalue – значение

8

Словарь (Dictionary)

Dictionary<TKey, TValue>

Tkey – ключ

Tvalue – значение

9

public class Element

{

public string Symbol { get; set; } public string Name { get; set; } public int AtomicNumber { get; set; }

}

Dictionary<string, Element> elements = new Dictionary<string, Element>(); elements.Add("K", new Element() { Symbol = "K", Name = "Potassium", AtomicNumber = 19 });

elements.Add("Ca", new Element() { Symbol = "Ca", Name = "Calcium", AtomicNumber = 20 });

10

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]