Back
Close

C# Professional - Collections - Exercises

talent-agile
8,285 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
·// { autofold
using System;
using System.Collections.Generic;
namespace Collections.Exercises
{
public class ListStub
{
// }
public static List<string> ProcessToKill(List<string> process)
{
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
·// { autofold
using System;
using System.Collections.Generic;
using System.Text;
namespace Collections.Exercises
{
public class DictionaryStub
{
// }
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();

// { autofold
using System;
using System.Collections.Generic;
namespace Collections.Samples
{
public class StackSample
{
public static void Main()
{
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Queue

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

Syntax

Queue obj = new Queue();

// { autofold
using System;
using System.Collections.Generic;
namespace Collections.Samples
{
public class QueueSample
{
public static void Main()
{
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