|
1 | 1 | ```Cs |
| 2 | +// Array |
| 3 | +
|
2 | 4 | // ------------------------------------ |
3 | 5 | // Declaration & Initialization |
4 | 6 | // ------------------------------------ |
5 | | -
|
6 | | -// TODO |
| 7 | +int[] myArray = new int[5]; |
| 8 | +int[] myArray = { 1, 2, 3 }; |
| 9 | +var myArray = new[] { 1, 2, 3 }; |
7 | 10 |
|
8 | 11 | // Const Declaration |
9 | | -// TODO |
| 12 | +// Arrays are mutable. Use readonly fields or immutable collections when needed. |
| 13 | +readonly int[] myArray = { 1, 2, 3 }; |
10 | 14 |
|
11 | 15 | // ------------------------------------ |
12 | 16 | // Direct Access |
13 | 17 | // ------------------------------------ |
14 | | -// TODO |
| 18 | +myArray[0] = 14; |
| 19 | +int value = myArray[0]; |
15 | 20 |
|
16 | 21 | // ------------------------------------ |
17 | 22 | // Push & Pop |
18 | 23 | // ------------------------------------ |
19 | | -// TODO |
| 24 | +// Not Available for fixed-size arrays. |
| 25 | +// Use List<T> for dynamic collections. |
20 | 26 |
|
21 | 27 | // ------------------------------------ |
22 | 28 | // Size / Length |
23 | 29 | // ------------------------------------ |
24 | | -// TODO |
| 30 | +int length = myArray.Length; |
25 | 31 |
|
26 | 32 | // ------------------------------------ |
27 | 33 | // Count (How many of) |
28 | 34 | // ------------------------------------ |
| 35 | +int howMany = myArray.Count(element => element == target); |
29 | 36 |
|
30 | 37 | // ------------------------------------ |
31 | 38 | // Filter |
32 | 39 | // ------------------------------------ |
33 | | -// TODO |
| 40 | +int[] filtered = myArray.Where(element => element > 10).ToArray(); |
34 | 41 |
|
35 | 42 | // ------------------------------------ |
36 | 43 | // Find (return Index) |
37 | 44 | // ------------------------------------ |
38 | | -// TODO |
| 45 | +int index = Array.IndexOf(myArray, target); |
| 46 | +int index = Array.FindIndex(myArray, element => element > 10); |
39 | 47 |
|
40 | 48 | // ------------------------------------ |
41 | 49 | // Accumulate (sum all values) |
42 | 50 | // ------------------------------------ |
43 | | -// TODO |
| 51 | +int total = myArray.Sum(); |
| 52 | +int total = myArray.Aggregate(0, (result, element) => result + element); |
44 | 53 |
|
45 | 54 | // ------------------------------------ |
46 | 55 | // Sort |
47 | 56 | // ------------------------------------ |
48 | | -// TODO |
| 57 | +Array.Sort(myArray); |
| 58 | +Array.Sort(myArray, (first, second) => second.CompareTo(first)); |
49 | 59 |
|
50 | 60 | // ------------------------------------ |
51 | 61 | // Comparing |
52 | 62 | // ------------------------------------ |
53 | | -// TODO |
| 63 | +bool isEqual = myArray.SequenceEqual(otherArray); |
54 | 64 |
|
55 | 65 | // ------------------------------------ |
56 | 66 | // Copy / Clone |
57 | 67 | // ------------------------------------ |
58 | | -// TODO |
| 68 | +int[] copy = myArray.ToArray(); |
| 69 | +int[] copy = (int[])myArray.Clone(); |
59 | 70 |
|
60 | 71 | // ------------------------------------ |
61 | 72 | // Slice |
62 | 73 | // ------------------------------------ |
63 | | -// TODO |
| 74 | +int[] slice = myArray[1..3]; // Since C# 8.0 |
64 | 75 |
|
65 | 76 | // ------------------------------------ |
66 | 77 | // Printing Members |
67 | 78 | // ------------------------------------ |
68 | | -// TODO |
| 79 | +Console.WriteLine(string.Join(", ", myArray)); |
| 80 | + |
| 81 | +foreach (int element in myArray) |
| 82 | +{ |
| 83 | + Console.WriteLine(element); |
| 84 | +} |
69 | 85 |
|
70 | 86 | // ------------------------------------ |
71 | 87 | // As a function Parameter |
72 | 88 | // ------------------------------------ |
73 | | -// TODO |
| 89 | +void MyFunction(int[] values) |
| 90 | +{ |
| 91 | + // ... |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +```Cs |
| 96 | +// List |
| 97 | +
|
| 98 | +List<int> myList = new List<int>(); |
| 99 | +List<int> myList = new() { 1, 2, 3 }; |
| 100 | + |
| 101 | +myList[0] = 14; |
| 102 | +myList.Add(10); |
| 103 | +myList.Insert(0, 10); |
| 104 | +myList.RemoveAt(0); |
| 105 | +myList.Remove(10); |
| 106 | + |
| 107 | +int length = myList.Count; |
| 108 | +int howMany = myList.Count(element => element == target); |
| 109 | +List<int> filtered = myList.Where(element => element > 10).ToList(); |
| 110 | +int index = myList.FindIndex(element => element > 10); |
| 111 | +int total = myList.Sum(); |
| 112 | + |
| 113 | +myList.Sort(); |
| 114 | +myList.Sort((first, second) => second.CompareTo(first)); |
| 115 | + |
| 116 | +bool isEqual = myList.SequenceEqual(otherList); |
| 117 | +List<int> copy = new List<int>(myList); |
| 118 | +List<int> slice = myList.GetRange(1, 2); |
| 119 | + |
| 120 | +Console.WriteLine(string.Join(", ", myList)); |
| 121 | + |
| 122 | +void MyFunction(List<int> values) |
| 123 | +{ |
| 124 | + // ... |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +```Cs |
| 129 | +// Dictionary |
| 130 | +
|
| 131 | +Dictionary<string, int> myDictionary = new Dictionary<string, int>(); |
| 132 | +Dictionary<string, int> myDictionary = new() |
| 133 | +{ |
| 134 | + ["first"] = 1, |
| 135 | + ["second"] = 2, |
| 136 | +}; |
| 137 | + |
| 138 | +myDictionary["third"] = 3; |
| 139 | +int value = myDictionary["first"]; |
| 140 | +bool hasKey = myDictionary.ContainsKey("first"); |
| 141 | +bool found = myDictionary.TryGetValue("first", out int value); |
| 142 | + |
| 143 | +myDictionary.Remove("first"); |
| 144 | +int length = myDictionary.Count; |
| 145 | + |
| 146 | +foreach (KeyValuePair<string, int> item in myDictionary) |
| 147 | +{ |
| 148 | + Console.WriteLine($"{item.Key}: {item.Value}"); |
| 149 | +} |
74 | 150 | ``` |
0 commit comments