-
Notifications
You must be signed in to change notification settings - Fork 8
/
ArrayInitialization.cs
101 lines (88 loc) · 2.68 KB
/
ArrayInitialization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
namespace Arrays;
public static class ArrayInitialization
{
public static void Laborious()
{
var workingWeekDayNames = new string[5];
workingWeekDayNames[0] = "Monday";
workingWeekDayNames[1] = "Tuesday";
workingWeekDayNames[2] = "Wednesday";
workingWeekDayNames[3] = "Thursday";
workingWeekDayNames[4] = "Friday";
}
public static void CollectionExpressions()
{
string[] workingWeekDayNames =
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
string[] weekDayNames = ["Sunday", .. workingWeekDayNames, "Saturday"];
Console.WriteLine(string.Join(", ", weekDayNames));
}
public static void InitializerExpression()
{
var workingWeekDayNames = new string[]
{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
}
#pragma warning disable IDE0300 // Simplify collection initialization - the following examples illustrate the older syntax, so we don't want to be told we can change these to collection expressions
public static void ShorterInitializer()
{
string[] workingWeekDayNames =
{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
}
public static void ElementTypeInference()
{
var workingWeekDayNames = new[]
{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
}
public static void ArrayInitializerArgument()
{
SetHeaders(new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" });
}
public static void JaggedCollectionExpression()
{
int[][] arrays =
[
[1, 2],
[1, 2, 3, 4, 5, 6],
[1, 2, 4],
[1],
[1, 2, 3, 4, 5]
];
}
public static void JaggedArrayInitializer()
{
var arrays = new int[5][]
{
new[] { 1, 2 },
new[] { 1, 2, 3, 4, 5, 6 },
new[] { 1, 2, 4 },
new[] { 1 },
new[] { 1, 2, 3, 4, 5 }
};
}
public static void Rectangular()
{
int[,] grid = new int[5, 10];
var smallerGrid = new int[,]
{
{ 1, 2, 3, 4 },
{ 2, 3, 4, 5 },
{ 3, 4, 5, 6 },
};
var cuboid = new int[,,]
{
{
{ 1, 2, 3, 4, 5 },
{ 2, 3, 4, 5, 6 },
{ 3, 4, 5, 6, 7 }
},
{
{ 2, 3, 4, 5, 6 },
{ 3, 4, 5, 6, 7 },
{ 4, 5, 6, 7, 8 }
},
};
}
private static void SetHeaders(IEnumerable<string> v)
{
}
}