You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `'class'`, `'struct'`, `'unmanaged'`, `'notnull'`, and `'default'` constraints cannot be combined or duplicated, and must be specified first in the constraints list.
14
14
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).
16
19
17
20
## Example
18
21
@@ -25,13 +28,19 @@ public interface I {} // Made public to avoid CS0703
25
28
26
29
publicclassC4
27
30
{
28
-
publicvoidF1<T>() whereT : class, struct, I {} // CS0449
29
-
publicvoidF2<T>() whereT : I, struct {} // CS0449
30
-
publicvoidF3<T>() whereT : I, class {} // CS0449
31
+
publicvoidF1<T>() whereT : class, struct, I {} // CS0449 - cannot combine class and struct
32
+
publicvoidF2<T>() whereT : I, struct {} // CS0449 - struct must be first
33
+
publicvoidF3<T>() whereT : I, class {} // CS0449 - class must be first
34
+
publicvoidF4<T>() whereT : class, notnull {} // CS0449 - class already implies notnull in nullable context
0 commit comments