C# Generic Classes

Summary: in this tutorial, you’ll learn how to use C# generics to define reusable and type-neutral classes.

Introduction to the C# generic classes

The following shows the Stack class that allows you to push a string onto a stack or pop the top element out of it:

namespace CSharpTutorial;

class Stack
{
    int current = -1;

    string[] items;

    public Stack(int size)
    {
        items = new string[size];
    }

    public bool Empty => current == -1;

    public bool Full => current == items.Length - 1;

    public bool Push(string item)
    {
        if (!Full)
        {
            items[++current] = item;
            return true;
        }
        return false;
    }

    public string? Pop() => !Empty ? items[current--] : null;

}Code language: C# (cs)

The Stack class works fine with strings. But if you want to have a stack of other types like integer, float, or objects, you need to create a class for each type.

To make the Stack class more generic and reusable, you can use C# generics to define a generic class. The following defines a generic Stack class that works with any type.

namespace CSharpTutorial;

class Stack<T>
{
    int current = -1;

    T[] items;

    public Stack(int size)
    {
        items = new T[size];
    }

    public bool Empty => current == -1;

    public bool Full => current == items.Length - 1;

    public bool Push(T item)
    {
        if (!Full)
        {
            items[++current] = item;
            return true;
        }
        return false;
    }

    public T? Pop() => !Empty ? items[current--] : default(T);

}Code language: C# (cs)

How it works.

First, specify the type (T) inside angle brackets <> that follows the class name:

class Stack<T>Code language: C# (cs)

Second, use the type (T) for the items member:

T[] items;Code language: C# (cs)

Third, use the type (T) in the constructor, pop, and push methods.

The following shows how to use the Stack<T> class:


class Program
{
    public static void Main(string[] args)
    {
        var colors = new Stack<string>(3);

        colors.Push("Red");
        colors.Push("Green");
        colors.Push("Blue");

        while (true)
        {
            var color = colors.Pop();

            if (color == null)
            {
                break;
            }

            Console.WriteLine(color);
        }

    }

}Code language: C# (cs)

How it works.

First, create a new instance of the Stack and specify the string as the type:

var colors = new Stack<string>(3);Code language: C# (cs)

Second, call the Push() method three times to push the Red, Green, and Blue strings into the stack:

colors.Push("Red");
colors.Push("Green");
colors.Push("Blue");Code language: C# (cs)

Third, pop the string out of the stack until it is empty by calling the Pop() method:

while (true)
{
    var color = colors.Pop();

    if (color == null)
    {
        break;
    }

    Console.WriteLine(color);
}Code language: C# (cs)

Summary

  • Use C# generic class to build reusable and type-neutral classes.
Was this tutorial helpful ?