Summary - Generics basics
Combine generic class and methods to write a Stack<T>.
Summary: the generics toolkit
In this group you learned generic classes, generic methods, type constraints, multi-parameter generics and the generic-vs-object difference. Now you will combine them by writing your own Stack<T> class.
A stack works by last-in first-out (LIFO). Push adds an item, Pop takes the most recently added one. Stack<int> and Stack<string> come from the same template but hold different types safely. Generic class design makes sense on a real data structure.
Exercise Define the Stack<T> class
Define a Stack<T> generic class. Private field: private List<T> _items = new List<T>();. Methods: public void Push(T item) does _items.Add(item);; public T Pop() reads the last item, removes it with _items.RemoveAt(_items.Count - 1); and does return last;. In Main create a Stack<int>; call Push(10), Push(20); print Pop().
Show example solution
Write class Stack<T> { private List<T> _items = new List<T>(); public void Push(T item) { _items.Add(item); } public T Pop() { T last = _items[_items.Count - 1]; _items.RemoveAt(_items.Count - 1); return last; } }. In Main add var s = new Stack<int>(); s.Push(10); s.Push(20); Console.WriteLine(s.Pop());
Example solution:
using System;
using System.Collections.Generic;
class Stack<T>
{
private List<T> _items = new List<T>();
public void Push(T item) { _items.Add(item); }
public T Pop()
{
T last = _items[_items.Count - 1];
_items.RemoveAt(_items.Count - 1);
return last;
}
}
class Program
{
static void Main()
{
var s = new Stack<int>();
s.Push(10);
s.Push(20);
Console.WriteLine(s.Pop());
}
}
Exercise Try Stack<string> too
Reuse the Stack<T> class above. In Main also create a Stack<string> instance; call Push("Alex") and Push("Sam"); print Pop() with Console.WriteLine.
Show example solution
In Main add var names = new Stack<string>(); names.Push("Alex"); names.Push("Sam"); Console.WriteLine(names.Pop());
Example solution:
using System;
using System.Collections.Generic;
class Stack<T>
{
private List<T> _items = new List<T>();
public void Push(T item) { _items.Add(item); }
public T Pop()
{
T last = _items[_items.Count - 1];
_items.RemoveAt(_items.Count - 1);
return last;
}
}
class Program
{
static void Main()
{
var s = new Stack<int>();
s.Push(10);
s.Push(20);
Console.WriteLine(s.Pop());
var names = new Stack<string>();
names.Push("Alex");
names.Push("Sam");
Console.WriteLine(names.Pop());
}
}
Generics basics complete
Congratulations! You now know generic classes and methods, type constraints and the generic-vs-object difference. You wrote your own Stack<T>; the same template worked type-safely with both int and string.
In the next group we will dig into generic collections: the capacity mechanics of List<T>, the hash structure of Dictionary<K, V> and the FIFO/LIFO behavior of Queue and Stack. Generics basics are the key to understanding how these collections work.
Test yourself
-
How does Stack<T> work?
- FIFO (first in first out)
- LIFO (last in first out)
- Alphabetical order
- Random order
Show answer
LIFO (last in first out) — Correct. A stack works by LIFO.
-
The stack type parameter
class Stack<____> { }Show answer
class Stack<T> { }