In the New Year I've decided that in order to become a better developer I need to expand my knowledge of C# and have begun to trawl through the Microsoft Documentation to help me do so.
I believe this should improve my problem solving abilities and provide me with new tools and ways of thinking. It seems like a no-brainer.
During my searching I've recently "discovered" a few things I had never seen before.
I say discovered but its not like they were exactly hidden - I'd just never needed these things before. Those things are Stack
and Queue
.
What are they ?
Simply put they're both types of Collections, they're just more limited in how you access the elements in them, we'll see both below :
Stack
Stack is a type of Collection that stores objects on a basis of last in first out (LIFO).
You can only retrieve and / or remove items at the end of the stack, and items added will be added at the end.
No exceptions.
It is recommended to use the generic Stack from System.Collection.Generic
namespace. rather than non-generic type.
Methods
Clear - Remove all objects from the Stack.
Contains - Used to check if an item is in the Stack.
Push - Adds an item to the end of the Stack.
Pop - Removes an item from the end of the Stack. If the Stack is empty it will throw an error.
Peek - Returns the item at the end of the Stack without removing it .
This method list is not complete, just the most relevant in my opinion - there are others like ToArray()
, ToString()
etc but these are common to other Collection types as well so theres no need to go into detail.
//Initialize
Stack<int> example = new Stack<int>();
//Add to Stack
example.Push(1);
example.Push(2);
example.Push(3);
//View last added element in Stack
int p1 = example.Peak();
//Get and remove the last added item from the Stack
int p2 = example.Pop();
Queue
Queue is the opposite of this - it operates on the basis of first in first out (FIFO).
You can think of it like people queuing for tickets - only those at the front can buy and leave the queue,
while anyone added to the queue is adde to the end.
It is recommended to use the generic Queue from System.Collection.Generic
namespace. rather than non-generic type.
Methods
Clear - Remove all objects from the
Contains - Used to check if an item is in the Queue
Dequeue - Removes an item from the begining of the Queue
Enqueue - Adds an item to the end of the Queue
Peek - Returns the item at the start of the Queue without removing it .
This method list is not complete, just the most relevant in my opinion - there are others like ToArray()
, ToString()
etc but these are common to other Collection types as well so theres no need to go into detail.
//Initialize Queue
Queue<int> example = new Queue<int>();
//Add to Queue
example.Enqueue(1);
example.Enqueue(2);
example.Enqueue(3);
//View first element in Queue
int p1 = example.Peak();
//Get and remove the first element in the Queue
int p2 = example.Dequeue();
Ok now what ?
Now I know what you're thinking after reading the above
"Why can't I just use a List
anyway"
Well you can if you want, and there's nothing wrong with it. But using Stacks and Queues both limits the access direction and I think most importantly it displays the intent of how the collection should be used to your fellow programmers.
That intent I think is the real gem here.
Adios
Well that's it, they're straight forward enough.
Feel free to ask questions, comment or contribute below!
And if you're feeling generous you can buy me a coffee with the link below ( and yes its all for coffee, I drink a copious amount of it while writing โ )
Top comments (0)