71 lines
1.6 KiB
Markdown
71 lines
1.6 KiB
Markdown
<center><img src="https://i.imgur.com/UWw1g20.png" /></center>
|
|
|
|
## System Collections
|
|
We have 3 collections namespace in C#
|
|
- System.Collections
|
|
- Store items as Object
|
|
- ArrayList
|
|
- Queue
|
|
- Stack
|
|
- Hashtable
|
|
- System.Collections.Generic
|
|
- Using Generics to specify one data type
|
|
- List
|
|
- Queue
|
|
- Stack
|
|
- Dictionary<TKey,TValue>
|
|
- System.Collections.Concurrent
|
|
- As previous bu thread safe
|
|
|
|
## Live coding - List
|
|
```csharp
|
|
namespace Lecture;
|
|
public static class Program {
|
|
public static void Main() {
|
|
var myList = new List<int>();
|
|
myList.Add(12);
|
|
myList.Add(23);
|
|
myList.Add(122);
|
|
myList.Add(312);
|
|
myList.Add(142);
|
|
|
|
myList[4] = 1;
|
|
|
|
int a = myList[5];
|
|
|
|
Console.WriteLine($"{myList}"); // Will return the type of the list and the value
|
|
Console.WriteLine(a); // Will return the value a (myList[5])
|
|
foreach (var element in myList) {
|
|
Console.WriteLine($"{element}");
|
|
} // Will display each element of the list
|
|
myList.ForEach(x => Console.WriteLine($"{x}")); // Same thing that foreach bellow
|
|
|
|
var t = myList.GetRange(3, 2);
|
|
t.ForEach(x => Console.WriteLine($"{x}"));
|
|
myList.sort();
|
|
myList.
|
|
}
|
|
|
|
}
|
|
```
|
|
|
|
## Live coding - Dictionary
|
|
```csharp
|
|
namespace Lecture;
|
|
public static class Program {
|
|
public static void Main() {
|
|
var dict = Dictionary<int, string>();
|
|
dict.Add(3, "tree");
|
|
dict.Add(9, "nine");
|
|
dict[9] = "nine";
|
|
dict[4] = "four";
|
|
dict.Add(4, "quatre"); // Raise an exception because key 4 exists
|
|
Console.WriteLine($"{dict[4]}"); // Return four
|
|
foreach (var kv in dict) {
|
|
Console.WriteLine($"dict[{kv.Key}] == {key.value}");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
``` |