skip to content

In C#, why does overriding `abstract void Record<T>(T? entry)` fail to compile unless the override adds `where T : default`?

level: seniorimportance: nice to knowfreq 18%

answer

  1. overrides inherit their constraints
  2. T? read in the override's own text
  3. class, struct or neither
  4. a C# 9 keyword in the where slot

basics

~20 s

An override inherits its constraints, but the compiler reads T? in its own signature; with no clause that means Nullable<T>, so nothing matches (CS0453, CS0115). where T : default (C# 9) says the base T has neither class nor struct.

solid answer

~40 s

Override and explicit interface implementation methods inherit their constraints from the base method and may not restate them (CS0460), with one exception: they may write `class` or `struct`, and since C# 9 `default`, because those decide what `T?` means in the override's own signature. With no clause, the compiler reads `T?` as `Nullable<T>`, which requires a value type, so an override of an unconstrained `Record<T>(T? entry)` reports CS0453 and CS0115 (no suitable method to override). `where T : class` would claim a reference type the base never required (CS8665). `where T : default` says the base parameter has neither constraint, so `T?` keeps its base meaning and the override binds. It is valid nowhere else (CS8823), and writing it against a `class` or `struct` base is CS8822.

code

csharp · 17 lines
csharp
#nullable enable
AuditLog log = new ConsoleAuditLog();
log.Record<string>(null);
log.Record<int>(0);
log.Record<int?>(null);

abstract class AuditLog
{
    public abstract void Record<T>(T? entry);
}

class ConsoleAuditLog : AuditLog
{
    // Without 'where T : default', T? here means Nullable<T>: CS0453 and CS0115
    public override void Record<T>(T? entry) where T : default
        => Console.WriteLine(entry is null ? "(no entry)" : $"{typeof(T).Name}: {entry}");
}

go deeper

for a junior

Recall that an override takes its generic constraints from the base method and does not repeat them.

for a middle

Explain that T? means Nullable<T>, a nullable reference or the unconstrained form depending on class, struct or neither, and that an override must say which.

for a senior

Diagnose CS0453 plus CS0115 on an override as a missing where T : default, and know its limits: CS8823 outside overrides and explicit implementations, CS8822 against a constrained base.

for a principal

When designing a generic base API, notice that T? on an unconstrained parameter forces every implementer to write where T : default, and weigh that against the clearer contract.

## Overrides take their constraints from the base When a method overrides a generic virtual or abstract method, or explicitly implements a generic interface method, it does **not** declare its own constraints. It inherits them, and restating them is an error: - writing `where T : IDisposable, new()` on an override is **CS0460**: "Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly, except for either a 'class', or a 'struct' constraint." The exception in that message exists because of one piece of syntax: `T?`. ## Why `T?` needs help in an override `T?` means different things depending on what is known about `T`: | What the compiler knows about `T` | `T?` means | |---|---| | `where T : struct` | `Nullable<T>` | | `where T : class` | a nullable reference to `T` | | neither | the unconstrained form, `T` marked as possibly default (its run-time meaning belongs to the lesson on nullable annotations) | In an override, the inherited constraints are not written down, yet the compiler must still decide what `T?` means from the override's own text. With no `where` clause it reads `T?` as `Nullable<T>`, the reading C# used before unconstrained `T?` was allowed. That interpretation demands a value type, so for an unconstrained base: ```csharp abstract class AuditLog { public abstract void Record<T>(T? entry); } class ConsoleAuditLog : AuditLog { public override void Record<T>(T? entry) { } // CS0453, CS0115, CS0534 } ``` The SDK reports **CS0453** (T must be a non-nullable value type to be used in `Nullable<T>`), **CS0115** ("no suitable method found to override") and **CS0534** (the abstract member is not implemented). ## The three ways to say what the base had 1. **No clause.** `T?` means `Nullable<T>`; this matches a base declared `where T : struct`. 2. **`where T : class`** (allowed in overrides since C# 8). `T?` is a nullable reference; it matches a base declared `where T : class`. Against a base without it, the SDK reports **CS8665** ("specifies a 'class' constraint for type parameter 'T', but corresponding type parameter 'T' of overridden or explicitly implemented method ... is not a reference type"). 3. **`where T : default`** (C# 9). `T?` is the unconstrained form; it matches a base with neither `class` nor `struct`. It does not add a constraint: it states that there is none of those two. `default` has guard rails of its own: - outside an override or explicit interface implementation it is **CS8823**: "The 'default' constraint is valid on override and explicit interface implementation methods only."; - against a base that is `class`- or `struct`-constrained it is **CS8822**; - it takes the primary slot, so it cannot sit beside `class` or `struct` (CS0449). ## Explicit interface implementations The same rules apply when a class implements a generic interface method explicitly: ```csharp interface IStore { void Put<T>(T? value); } class Store : IStore { void IStore.Put<T>(T? value) where T : default { } } ``` Without `where T : default`, the explicit implementation again reads `T?` as `Nullable<T>` and no longer matches the interface member. ## Practical notes - The problem appears only when the base signature uses `T?` on an unconstrained `T`. An override of `Record<T>(T entry)` needs no clause at all. - In review, `where T : default` on an override is load-bearing: deleting it as noise brings back CS0453 and CS0115. - Restating `struct` on an override of a `struct`-constrained base is permitted but redundant, since no clause already means `Nullable<T>` there. - None of this changes what the override's callers may pass: the constraints are still the base method's. ## Diagnosing it in practice The errors arrive as a cluster that looks unrelated to constraints: 1. CS0453 on the `T?` parameter, because the override's `T?` was read as `Nullable<T>`; 2. CS0115 on the method name, because a `Nullable<T>` parameter matches no base method; 3. CS0534 on the class, because the abstract member is now unimplemented. All three disappear once `where T : default` is added. If the base method is later changed to `where T : class`, the same override reports CS8822 and should switch to `where T : class`.

  • Where besides an override may C# code write `where T : default`?
    Only on an explicit interface implementation, such as `void IStore.Put<T>(T? value) where T : default`. Anywhere else the SDK reports CS8823. Against a base or interface method that is constrained to `class` or `struct`, it reports CS8822, because `default` claims the base has neither.
  • Why may a C# override write `class` or `struct` in a where clause but not `IDisposable` or `new()`?
    An override inherits every constraint from the base method, so restating them would be redundant at best and conflicting at worst: CS0460. `class` and `struct` are allowed (since C# 8) only because they decide how `T?` in the override's signature is read; `default` (C# 9) covers the remaining case, a base with neither.

  • An override must repeat all of the base method's constraints.
  • where T : default makes T a value type, so T? becomes Nullable<T>.
  • where T : default can be put on any generic method to mean no constraint.
  • Adding where T : class to the override is a safe fix for an unconstrained base.
  • The default constraint means T falls back to object when no type argument is given.