Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Generic constraints in C#

Reference for C# generic constraints: where clauses for class, struct, new(), notnull, unmanaged, base types, interfaces, allows ref struct and static abstract members.

A constraint limits which type arguments a generic type or method accepts, and in return lets the body use members that the constraint guarantees. Without constraints, T is treated as object: you can call ToString() and compare with == against null, but nothing more.

Constraints are written with where clauses after the parameter list. A type parameter can have several constraints, listed in a fixed order, and each type parameter has its own where.

C#
T Max<T>(T a, T b) where T : IComparable<T> => a.CompareTo(b) >= 0 ? a : b;

Try it

C# Interface, base-class and new() constraints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# class, struct, notnull and static abstract members

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
class Repo<TKey, TEntity>
    where TKey : notnull, IEquatable<TKey>
    where TEntity : class, IEntity, new()
{ }
Constraint Meaning
where T : class Reference type (nullable-aware: class? allows nullable references).
where T : struct Non-nullable value type. Implies new().
where T : notnull Non-nullable type, reference or value.
where T : unmanaged Struct with no reference fields at any depth; allows pointers and sizeof.
where T : new() Public parameterless constructor; allows new T(). Must come last.
where T : BaseClass Is or derives from that class (at most one, and it must be listed first).
where T : IFoo Implements the interface; several allowed. May use static abstract members.
where T : U Is or derives from another type parameter U (naked type constraint).
where T : default Disambiguates unconstrained T? in overrides.
where T : allows ref struct C# 13: T may be a ref struct such as Span<int>.

Order: primary constraint (class, struct, notnull, unmanaged or a base class) first, then interfaces, then new() last.

Errors: CS0452 ("the type 'int' must be a reference type in order to use it as parameter 'T'") when a type argument violates a constraint; CS0310 when new() is not satisfied; CS0701 for invalid constraint types such as sealed classes, object or System.ValueType.

Usage notes

  • Constraints are part of the contract. Callers fail at compile time if the argument does not fit, which is better than a runtime cast failure.
  • Interface constraints avoid boxing. Calling a.CompareTo(b) on a struct T : IComparable<T> is a constrained call, not a box.
  • No constraint for operators. Use INumber<T>, IAdditionOperators<T,T,T> and friends from generic math instead of a "has +" rule.
  • No constructor-with-arguments constraint. Pass a Func<..., T> factory when you need to build T from data.
  • Enums and delegates. Since C# 7.3 you can constrain to System.Enum (write where T : struct, Enum), System.Delegate and System.MulticastDelegate.
  • Overrides repeat nothing. Overriding methods and explicit interface implementations inherit the constraints; only class, struct and default may be restated.
  • Too many constraints are a smell. If you need five, an interface that bundles the requirements may express the idea better.