Skip to content

Update CS0449 documentation to include all constraint types #47687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions docs/csharp/misc/cs0449.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: "Compiler Error CS0449"
title: "Compiler Error CS0449"
ms.date: 07/20/2015
ms.date: 08/01/2025
f1_keywords:
- "CS0449"
helpviewer_keywords:
Expand All @@ -12,7 +12,10 @@ ms.assetid: 32c07a2c-4c48-4d07-b643-72422a6b9fac

The `'class'`, `'struct'`, `'unmanaged'`, `'notnull'`, and `'default'` constraints cannot be combined or duplicated, and must be specified first in the constraints list.

The constraints on the type parameter of a generic type or method must occur in a specific order: `class` or `struct` must be first, if present, then any interface constraints, and finally any constructor constraints. This error is caused by the `class` or `struct` constraint not appearing first. To resolve this error, reorder the constraint clauses.
The constraints on the type parameter of a generic type or method must occur in a specific order. You can apply at most one of the `class`, `struct`, `unmanaged`, `notnull`, or `default` constraints, and if you specify any of these constraints, it must be the first constraint specified for that type parameter. These are followed by interface constraints, and finally any constructor constraints. This error is caused by one of these primary constraints (`class`, `struct`, `unmanaged`, `notnull`, or `default`) not appearing first, or by attempting to combine multiple primary constraints. To resolve this error, reorder the constraint clauses or remove duplicate constraints.

> [!NOTE]
> In a nullable context, the `class` constraint already implies `notnull`, so they cannot be combined. For comprehensive information about all constraint types and their rules, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md).

## Example

Expand All @@ -25,13 +28,19 @@ public interface I {} // Made public to avoid CS0703

public class C4
{
public void F1<T>() where T : class, struct, I {} // CS0449
public void F2<T>() where T : I, struct {} // CS0449
public void F3<T>() where T : I, class {} // CS0449
public void F1<T>() where T : class, struct, I {} // CS0449 - cannot combine class and struct
public void F2<T>() where T : I, struct {} // CS0449 - struct must be first
public void F3<T>() where T : I, class {} // CS0449 - class must be first
public void F4<T>() where T : class, notnull {} // CS0449 - class already implies notnull in nullable context
public void F5<T>() where T : unmanaged, struct {} // CS0449 - cannot combine unmanaged and struct
public void F6<T>() where T : I, unmanaged {} // CS0449 - unmanaged must be first
public void F7<T>() where T : notnull, default {} // CS0449 - cannot combine notnull and default

// OK
public void F4<T>() where T : class {}
public void F5<T>() where T : struct {}
public void F6<T>() where T : I {}
public void F8<T>() where T : class {}
public void F9<T>() where T : struct {}
public void F10<T>() where T : unmanaged {}
public void F11<T>() where T : notnull {}
public void F12<T>() where T : I {}
}
```