Module 4 - Chapter 2: Generics in C#

Generics are one of the most powerful features of C#, enabling developers to define type-safe data structures, without committing to actual data types. This results in a more flexible, reusable, and type-safe codebase. Generics allow you to write a class or method that can work with any data type. When you use a class or method that has been defined generically, you specify the exact data type it should work with. This chapter explores the concept of generics in C#, including how to define and use generic classes, methods, interfaces, and delegates.

Mar 29, 2024

Generics in C#

Generics are one of the most powerful features of C#, enabling developers to define type-safe data structures, without committing to actual data types. This results in a more flexible, reusable, and type-safe codebase. Generics allow you to write a class or method that can work with any data type. When you use a class or method that has been defined generically, you specify the exact data type it should work with. This chapter explores the concept of generics in C#, including how to define and use generic classes, methods, interfaces, and delegates.

Why Use Generics?

Generics offer several benefits:
  • Type Safety: Generics enforce type safety at compile time, preventing runtime type errors and reducing the need for type casting.
  • Reusability: You can write a generic class or method that can work with any data type, making your code more reusable.
  • Performance: Generics reduce the need for boxing and unboxing when working with value types, improving performance.

Defining Generic Classes

A generic class is defined with a type parameter in angle brackets. This type parameter can then be used within the class as if it were a regular type.
public class GenericList<T> { private T[] items; private int count; public GenericList(int capacity) { items = new T[capacity]; } public void Add(T item) { items[count++] = item; } public T GetItem(int index) { return items[index]; } }
To use the GenericList class, you specify the type of elements it will store:
var listOfIntegers = new GenericList<int>(10); listOfIntegers.Add(1); var listOfStrings = new GenericList<string>(10); listOfStrings.Add("Hello");

Generic Methods

Methods can also be generic, defining their own type parameters:
public class Utility { public static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; } }
Use the generic method like this:
int a = 1, b = 2; Utility.Swap<int>(ref a, ref b); string x = "hello", y = "world"; Utility.Swap<string>(ref x, ref y);

Generic Interfaces and Delegates

Interfaces and delegates can also be generic, allowing for the definition of generic contracts and callbacks.

Generic Interface

public interface IRepository<T> { T FindById(int id); void Save(T item); }

Generic Delegate

public delegate void Action<T>(T item);

Constraints on Type Parameters

You can constrain the types that might be used with generics using constraints. This ensures that type arguments meet certain criteria.
public class GenericClass<T> where T : class, new() { public T CreateInstance() { return new T(); } }
In this example, T is constrained to reference types (class) that have a default constructor (new()).

Summary

Generics enhance the flexibility, reusability, and type safety of your code. By using generics, you can create data structures and algorithms that are decoupled from specific data types, making them more versatile and efficient. Understanding how to define and use generic classes, methods, interfaces, and delegates is crucial for writing modern, efficient, and maintainable C# code.