diff --git a/docs/csharp/misc/cs0449.md b/docs/csharp/misc/cs0449.md index 2a9902e33c071..728c9f21be044 100644 --- a/docs/csharp/misc/cs0449.md +++ b/docs/csharp/misc/cs0449.md @@ -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: @@ -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 @@ -25,13 +28,19 @@ public interface I {} // Made public to avoid CS0703 public class C4 { - public void F1() where T : class, struct, I {} // CS0449 - public void F2() where T : I, struct {} // CS0449 - public void F3() where T : I, class {} // CS0449 + public void F1() where T : class, struct, I {} // CS0449 - cannot combine class and struct + public void F2() where T : I, struct {} // CS0449 - struct must be first + public void F3() where T : I, class {} // CS0449 - class must be first + public void F4() where T : class, notnull {} // CS0449 - class already implies notnull in nullable context + public void F5() where T : unmanaged, struct {} // CS0449 - cannot combine unmanaged and struct + public void F6() where T : I, unmanaged {} // CS0449 - unmanaged must be first + public void F7() where T : notnull, default {} // CS0449 - cannot combine notnull and default // OK - public void F4() where T : class {} - public void F5() where T : struct {} - public void F6() where T : I {} + public void F8() where T : class {} + public void F9() where T : struct {} + public void F10() where T : unmanaged {} + public void F11() where T : notnull {} + public void F12() where T : I {} } ```