C# Professional - Collections - Exercises

talent-agile
32.7K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

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();

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content