0
0
CsharpHow-ToBeginner · 4 min read

How to Use Multiple Constraints in Generics in C#

In C#, you can use multiple constraints on a generic type by separating them with commas inside the where clause, like where T : class, new(). This lets you require that the type parameter meets several conditions, such as being a reference type and having a parameterless constructor.
📐

Syntax

The syntax for multiple constraints in C# generics uses the where keyword followed by the type parameter and a list of constraints separated by commas.

  • where T : constraint1, constraint2, ...
  • Constraints can include class, struct, interfaces, base classes, and new() for parameterless constructors.
csharp
public class ExampleClass<T> where T : class, IDisposable, new()
{
    public T CreateInstance()
    {
        return new T();
    }
}
💻

Example

This example shows a generic class that requires its type parameter to be a reference type (class), implement IDisposable, and have a parameterless constructor (new()).

csharp
using System;

public class ResourceHolder<T> where T : class, IDisposable, new()
{
    public T Resource { get; private set; }

    public ResourceHolder()
    {
        Resource = new T();
    }

    public void UseResource()
    {
        Console.WriteLine("Using resource...");
        Resource.Dispose();
        Console.WriteLine("Resource disposed.");
    }
}

public class MyResource : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("MyResource.Dispose called");
    }
}

class Program
{
    static void Main()
    {
        var holder = new ResourceHolder<MyResource>();
        holder.UseResource();
    }
}
Output
Using resource... MyResource.Dispose called Resource disposed.
⚠️

Common Pitfalls

Common mistakes when using multiple constraints include:

  • Forgetting to separate constraints with commas.
  • Placing the new() constraint anywhere but last.
  • Trying to use incompatible constraints together, like class and struct.
  • Not implementing required interfaces or constructors in the type argument.
csharp
/* Wrong: missing comma between constraints and wrong order of new() */
public class WrongExample<T> where T : class, new(), IDisposable
{
}

/* Right: commas between constraints and new() last */
public class RightExample<T> where T : class, IDisposable, new()
{
}
📊

Quick Reference

Summary tips for multiple constraints in C# generics:

  • Use where T : constraint1, constraint2, ... syntax.
  • class means reference type; struct means value type.
  • new() must be last and requires a parameterless constructor.
  • Constraints can include interfaces and base classes.

Key Takeaways

Use commas to separate multiple constraints in the where clause of generics.
The new() constraint must always come last in the list of constraints.
You cannot combine conflicting constraints like class and struct together.
Constraints can include interfaces, base classes, class/struct, and new().
Applying multiple constraints ensures safer and more predictable generic code.