Skip to content

Commit 532611d

Browse files
CopilotBillWagner
andauthored
Update CS0449 documentation to include all constraint types (#47687)
* Initial plan * Update CS0449 documentation to include all constraint types Co-authored-by: BillWagner <[email protected]> * Update date in CS0449.md to 08/01/2025 Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent 2b917e0 commit 532611d

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

docs/csharp/misc/cs0449.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Compiler Error CS0449"
33
title: "Compiler Error CS0449"
4-
ms.date: 07/20/2015
4+
ms.date: 08/01/2025
55
f1_keywords:
66
- "CS0449"
77
helpviewer_keywords:
@@ -12,7 +12,10 @@ ms.assetid: 32c07a2c-4c48-4d07-b643-72422a6b9fac
1212

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

15-
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.
15+
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.
16+
17+
> [!NOTE]
18+
> 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).
1619
1720
## Example
1821

@@ -25,13 +28,19 @@ public interface I {} // Made public to avoid CS0703
2528
2629
public class C4
2730
{
28-
public void F1<T>() where T : class, struct, I {} // CS0449
29-
public void F2<T>() where T : I, struct {} // CS0449
30-
public void F3<T>() where T : I, class {} // CS0449
31+
public void F1<T>() where T : class, struct, I {} // CS0449 - cannot combine class and struct
32+
public void F2<T>() where T : I, struct {} // CS0449 - struct must be first
33+
public void F3<T>() where T : I, class {} // CS0449 - class must be first
34+
public void F4<T>() where T : class, notnull {} // CS0449 - class already implies notnull in nullable context
35+
public void F5<T>() where T : unmanaged, struct {} // CS0449 - cannot combine unmanaged and struct
36+
public void F6<T>() where T : I, unmanaged {} // CS0449 - unmanaged must be first
37+
public void F7<T>() where T : notnull, default {} // CS0449 - cannot combine notnull and default
3138
3239
// OK
33-
public void F4<T>() where T : class {}
34-
public void F5<T>() where T : struct {}
35-
public void F6<T>() where T : I {}
40+
public void F8<T>() where T : class {}
41+
public void F9<T>() where T : struct {}
42+
public void F10<T>() where T : unmanaged {}
43+
public void F11<T>() where T : notnull {}
44+
public void F12<T>() where T : I {}
3645
}
3746
```

0 commit comments

Comments
 (0)