Back
Close

C# Professional - Collections - Exercises

talent-agile
2,571 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
·// {...}
public static List<string> ProcessToKill(List<string> process)
{
// Create list of string with initial size to 3.
List<string> processToKill = new List<string>(3);
// Show capacity ; here : 3.
Console.WriteLine(string.Format("Capacity {0}", processToKill.Capacity));
// Show number of items ; here : 0.
Console.WriteLine(string.Format("Count {0}", processToKill.Count));
/// TODO:
/// Add items from process to processToKill list
/// Process equals "Explorer.exe" don't be added, ignore it
foreach (var p in processToKill)
{
Console.WriteLine(p);
}
return processToKill;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
·// {...}
public class Employee : IEquatable<Employee>
{
public int Age;
public string Name;
public Employee(string name, int age)
{
this.Age = age;
this.Name = name;
}
public bool Equals(Employee other)
{
return (this.Age == other.Age && this.Name == other.Name);
}
}
public static Dictionary<int, List<string>> GetEmployeesByAge(List<Employee> employees)
{
var result = new Dictionary<int, List<string>>();
foreach (var e in employees)
{
///TODO: add employes to result,
/// the key must contain age and
/// values are names list who have the same age.
}
return result;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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();

// {...}
// Creates and initializes a new Stack.
Stack<string> myStack = new Stack<string>();
myStack.Push("Hello");
myStack.Push("World");
myStack.Push("!");
// Displays the properties and values of the Stack.
Console.WriteLine("\tCount: {0}", myStack.Count);
Console.Write("\tValues:");
foreach (var item in myStack)
{
Console.Write(" {0}", item);
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Queue

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

Syntax

Queue obj = new Queue();

// {...}
// Creates and initializes a new Queue.
Queue<string> myQ = new Queue<string>();
myQ.Enqueue("Hello");
myQ.Enqueue("World");
myQ.Enqueue("!");
// Displays the properties and values of the Queue.
Console.WriteLine("\tCount: {0}", myQ.Count);
Console.Write("\tValues:");
foreach(var item in myQ)
{
Console.Write(" {0}", item);
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants