Back
Close

C# Professional - Collections - Exercises

talent-agile
31.6K views

List

Syntax

List<T> obj = new List<T>();

Where "T" generic parameter you can pass any data-type or custom class object to this parameter.

Exercice
Solution

Replace TODO by this code:

foreach (var p in process) { if (!p.Equals("Explorer.exe")) { processToKill.Add(p); } }

Dictionary

Dictionary is a generic collections which works on key and value pairs. Both key and value pair can have different data-types or same data-types or any custom types (i.e. class objects). Dictionary generic collections is a generic concept applied over Hashtable and it provides fast lookups with keys to get values.

Syntax

Dictionary<T, T> obj = new Dictionary<T, T>();

Where "T" generic parameter you can pass any data-type or custom class object to this parameter.

Exercice
Solution

Replace TODO by this code: if(result.ContainsKey(e.Age)) { result[e.Age].Add(e.Name); } else { result.Add(e.Age, new List<string>() { e.Name }); }

LIFO vs FIFO

Stack

A stack is a collection of type Last In First Out ("LIFO").

Syntax

Stack obj = new Stack();

Queue

A queue is a collection of type First In First Out ("FIFO").

Syntax

Queue obj = new Queue();

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io