From 8fbf9cd1395c25e836d9cb8a80cf4389f2b0f16f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 11:51:57 -0500 Subject: [PATCH 01/12] Skeleton and first consolidation --- .github/prompts/error-consolidation.md | 4 +- .openpublishing.redirection.csharp.json | 5 ++ .../generic-type-parameters-errors.md | 78 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 7 +- 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 43a9e9da2f695..510d21a094614 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -12,8 +12,8 @@ Overall steps: We're going to work through a series of files consolidating errors and warnings. -- For the duration of this chat, all references to "destination file" refer to `explicit-interface-errors.md`. -- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to members that explicitly implement an interface member. +- For the duration of this chat, all references to "destination file" refer to `generic-type-parameters-errors.md`. +- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to generic type parameters and generic type arguments. The destination file already contains a skeleton for the final output. diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index c773c4e821a7b..f91d5a092b802 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -1,5 +1,10 @@ { "redirections": [ + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0304.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, { "source_path_from_root": "/redirections/proposals/csharp-7.0/binary-literals.md", "redirect_url": "/dotnet/csharp/language-reference/language-specification/lexical-structure#6453-integer-literals" diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md new file mode 100644 index 0000000000000..7baf635c5659a --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -0,0 +1,78 @@ +--- +title: Resolve errors and warnings related to generic type parameters and type arguments. +description: These compiler errors and warnings indicate errors in generic type parameters and type arguments. +f1_keywords: + - "CS0304" + - "CS9338" +helpviewer_keywords: + - "CS0304" + - "CS9338" +ms.date: 11/13/2025 +ai-usage: ai-assisted +--- +# Resolve errors and warnings related to generic type parameters and generic type arguments + +This article covers the following compiler errors: + + +- [**CS0304**](#new-constraint): *Cannot create an instance of the variable type 'type' because it does not have the new() constraint* +- [**CS0338**](#type-argument-visibility): *Inconsistent accessibility: type argument is less accessible than class.* + +## new() constraint + +When you implement a generic class, and you want to use the `new` keyword to create a new instance of any type that is supplied for a type parameter `T`, you must apply the [new() constraint](../keywords/new-constraint.md) to `T` in the class declaration, as shown in the following example. + +```csharp +class C where T : new() +``` + +The `new()` constraint enforces type safety by guaranteeing that any concrete type that is supplied for `T` has a parameterless constructor. CS0304 occurs if you attempt to use the `new` operator in the body of the class to create an instance of type parameter `T` when `T` does not specify the `new()` constraint. On the client side, if code attempts to instantiate the generic class with a type that has no parameterless constructor, that code will generate [Compiler Error CS0310](./cs0310.md). + +The following example generates CS0304. + +```csharp +// CS0304.cs +// Compile with: /target:library. +class C +{ + // The following line generates CS0304. + T t = new T(); +} +``` + +The `new` operator also is not allowed in methods of the class. + +```csharp +// Compile with: /target:library. +class C +{ + public void ExampleMethod() + { + // The following line generates CS0304. + T t = new T(); + } +} +``` + +To avoid the error, declare the class by using the `new()` constraint, as shown in the following example. + +```csharp +// Compile with: /target:library. +class C where T : new() +{ + T t = new T(); + + public void ExampleMethod() + { + T t = new T(); + } +} +``` + +## Type argument visibility + +The following errors relate to accessibilty of type arguments: + +- **CS0338**: *Inconsistent accessibility: type argument is less accessible than class.* diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 74f177bc16a86..bb7c86b4c1539 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -509,6 +509,11 @@ items: CS0182, CS0591, CS0599, CS0617, CS0633, CS0643, CS0655, CS0839, CS1016, CS1739, CS1740, CS1742, CS1744, CS1746, CS7036, CS7067, CS8196, CS8324, CS8861, CS8905, CS8943, CS8944, CS8945, CS8948, CS8949, CS8950, CS8951, CS8964, CS8965, CS8966 + - name: Generic type parameters and type arguments + href: ./compiler-messages/generic-type-parameters-errors.md + displayName: > + generic, type parameter, type argument, constraint, + CS0304, CS9338 - name: asynchronous methods href: ./compiler-messages/async-await-errors.md displayName: > @@ -963,8 +968,6 @@ items: href: ../misc/cs0281.md - name: CS0283 href: ../misc/cs0283.md - - name: CS0304 - href: ./compiler-messages/cs0304.md - name: CS0305 href: ../misc/cs0305.md - name: CS0306 From aea9f569fa003e4e13068b779446270d100fdcb2 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 13:09:20 -0500 Subject: [PATCH 02/12] Consolidate related errors Consolidate all related errors for type arguments and type parameters. --- .openpublishing.redirection.csharp.json | 95 +++ .../compiler-messages/cs0304.md | 66 -- .../compiler-messages/cs0310.md | 51 -- .../compiler-messages/cs0311.md | 45 - .../compiler-messages/cs0413.md | 45 - .../compiler-messages/cs0417.md | 35 - .../generic-type-parameters-errors.md | 570 ++++++++++++- docs/csharp/language-reference/toc.yml | 36 +- docs/csharp/misc/cs0080.md | 39 - docs/csharp/misc/cs0081.md | 35 - docs/csharp/misc/cs0305.md | 34 - docs/csharp/misc/cs0306.md | 30 - docs/csharp/misc/cs0307.md | 32 - docs/csharp/misc/cs0308.md | 54 -- docs/csharp/misc/cs0312.md | 45 - docs/csharp/misc/cs0313.md | 57 -- docs/csharp/misc/cs0314.md | 43 - docs/csharp/misc/cs0315.md | 46 -- docs/csharp/misc/cs0403.md | 40 - docs/csharp/misc/cs0412.md | 43 - docs/csharp/misc/cs0694.md | 38 - docs/csharp/misc/cs0695.md | 30 - docs/csharp/misc/cs0698.md | 24 - ...n-t-have-specifics-on-this-csharp-error.md | 781 +++++++++++++++++- 24 files changed, 1448 insertions(+), 866 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0304.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0310.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0311.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0413.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0417.md delete mode 100644 docs/csharp/misc/cs0080.md delete mode 100644 docs/csharp/misc/cs0081.md delete mode 100644 docs/csharp/misc/cs0305.md delete mode 100644 docs/csharp/misc/cs0306.md delete mode 100644 docs/csharp/misc/cs0307.md delete mode 100644 docs/csharp/misc/cs0308.md delete mode 100644 docs/csharp/misc/cs0312.md delete mode 100644 docs/csharp/misc/cs0313.md delete mode 100644 docs/csharp/misc/cs0314.md delete mode 100644 docs/csharp/misc/cs0315.md delete mode 100644 docs/csharp/misc/cs0403.md delete mode 100644 docs/csharp/misc/cs0412.md delete mode 100644 docs/csharp/misc/cs0694.md delete mode 100644 docs/csharp/misc/cs0695.md delete mode 100644 docs/csharp/misc/cs0698.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index f91d5a092b802..45dc2b1d642b0 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -1,10 +1,105 @@ { "redirections": [ + { + "source_path_from_root": "/docs/csharp/misc/cs0080.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0081.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0305.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0306.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0307.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0308.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0312.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0313.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0314.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0315.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0403.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0412.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0694.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0695.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0698.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0304.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", "redirect_document_id": false }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0310.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0311.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0413.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0417.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/generic-type-parameters-errors", + "redirect_document_id": false + }, { "source_path_from_root": "/redirections/proposals/csharp-7.0/binary-literals.md", "redirect_url": "/dotnet/csharp/language-reference/language-specification/lexical-structure#6453-integer-literals" diff --git a/docs/csharp/language-reference/compiler-messages/cs0304.md b/docs/csharp/language-reference/compiler-messages/cs0304.md deleted file mode 100644 index bf57111ea3961..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0304.md +++ /dev/null @@ -1,66 +0,0 @@ ---- -description: "Compiler Error CS0304" -title: "Compiler Error CS0304" -ms.date: 07/20/2015 -f1_keywords: - - "CS0304" -helpviewer_keywords: - - "CS0304" -ms.assetid: 22dc7211-57a8-4c92-96f6-fc1cf0263b68 ---- -# Compiler Error CS0304 - -Cannot create an instance of the variable type 'type' because it does not have the new() constraint - - When you implement a generic class, and you want to use the `new` keyword to create a new instance of any type that is supplied for a type parameter `T`, you must apply the [new() constraint](../keywords/new-constraint.md) to `T` in the class declaration, as shown in the following example. - -```csharp -class C where T : new() -``` - - The `new()` constraint enforces type safety by guaranteeing that any concrete type that is supplied for `T` has a parameterless constructor. CS0304 occurs if you attempt to use the `new` operator in the body of the class to create an instance of type parameter `T` when `T` does not specify the `new()` constraint. On the client side, if code attempts to instantiate the generic class with a type that has no parameterless constructor, that code will generate [Compiler Error CS0310](./cs0310.md). - - The following example generates CS0304. - -```csharp -// CS0304.cs -// Compile with: /target:library. -class C -{ - // The following line generates CS0304. - T t = new T(); -} -``` - - The `new` operator also is not allowed in methods of the class. - -```csharp -// Compile with: /target:library. -class C -{ - public void ExampleMethod() - { - // The following line generates CS0304. - T t = new T(); - } -} -``` - - To avoid the error, declare the class by using the `new()` constraint, as shown in the following example. - -```csharp -// Compile with: /target:library. -class C where T : new() -{ - T t = new T(); - - public void ExampleMethod() - { - T t = new T(); - } -} -``` - -## See also - -- [C# Compiler Errors](./index.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs0310.md b/docs/csharp/language-reference/compiler-messages/cs0310.md deleted file mode 100644 index 8411753e1eda8..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0310.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -description: "Compiler Error CS0310" -title: "Compiler Error CS0310" -ms.date: 07/20/2015 -f1_keywords: - - "CS0310" -helpviewer_keywords: - - "CS0310" -ms.assetid: f7db7e56-f51f-406f-a54b-48ea61b5cb3e ---- -# Compiler Error CS0310 - -The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic' - - The generic type or method defines the [`new()` constraint](../keywords/new-constraint.md) in its `where` clause, so any type must have a public parameterless constructor in order to be used as a type argument for that generic type or method. To avoid this error, make sure that the type has the correct constructor, or modify the constraint clause of the generic type or method. - -## Example - - The following sample generates CS0310: - -```csharp -// CS0310.cs -using System; - -class G where T : new() -{ - T t; - - public G() - { - t = new T(); - Console.WriteLine(t); - } -} - -class B -{ - private B() { } - // Try this instead: - // public B() { } -} - -class CMain -{ - public static void Main() - { - G g = new G(); // CS0310 - Console.WriteLine(g.ToString()); - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs0311.md b/docs/csharp/language-reference/compiler-messages/cs0311.md deleted file mode 100644 index 7f14c90edf21c..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0311.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -description: "Compiler Error CS0311" -title: "Compiler Error CS0311" -ms.date: 07/20/2015 -f1_keywords: - - "CS0311" -helpviewer_keywords: - - "CS0311" -ms.assetid: d095f0fa-efd7-491c-a80b-4c5704a90de7 ---- -# Compiler Error CS0311 - -The type 'type1' cannot be used as type parameter 'T' in the generic type or method '\'. There is no implicit reference conversion from 'type1' to 'type2'. - - When a constraint is applied to a generic type parameter, an implicit identity or reference conversion must exist from the concrete argument to the type of the constraint. - -## To correct this error - -1. Change the type argument you are using to one that fulfills the constraint. - -2. If you own the class, you can remove the constraint or else do something to enable an implicit reference or identity conversion. For example, you can make the second type inherit from the first. - -## Example - -```csharp -// cs0311.cs -class B {} -class C {} -class Test where T : C -{ } - -class Program -{ - static void Main() - { - Test test = new Test(); //CS0311 - } -} -``` - - If this error occurs when trying to use a value-type argument, notice that an implicit numeric conversion, for example from `short` to `int`, does not satisfy a generic type parameter. - -## See also - -- [Constraints on Type Parameters](../../programming-guide/generics/constraints-on-type-parameters.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs0413.md b/docs/csharp/language-reference/compiler-messages/cs0413.md deleted file mode 100644 index 0896efebfcc21..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0413.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -description: "Compiler Error CS0413" -title: "Compiler Error CS0413" -ms.date: 07/20/2015 -f1_keywords: - - "CS0413" -helpviewer_keywords: - - "CS0413" -ms.assetid: a01bd1ec-015b-433b-be55-b91db268d6a5 ---- -# Compiler Error CS0413 - -The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint - -This error occurs if a generic type uses the [as](../operators/type-testing-and-cast.md#the-as-operator) operator, but that generic type does not have a class type constraint. The `as` operator is only allowed with reference and nullable value types, so the type parameter must be constrained to guarantee that it is not a value type. To avoid this error, use a class type constraint or a reference type constraint. - -This is because the `as` operator could return `null`, which is not a possible value for a value type, and the type parameter must be treated as a value type unless it is a class type constraint or a reference type constraint. - -## Example - -The following sample generates CS0413. - -```csharp -// CS0413.cs -// compile with: /target:library -class A {} -class B : A {} - -class CMain -{ - A a = null; - public void G() - { - a = new A(); - System.Console.WriteLine (a as T); // CS0413 - } - - // OK - public void H() where T : A - { - a = new A(); - System.Console.WriteLine (a as T); - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs0417.md b/docs/csharp/language-reference/compiler-messages/cs0417.md deleted file mode 100644 index a162b9898aea5..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0417.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS0417" -title: "Compiler Error CS0417" -ms.date: 07/20/2015 -f1_keywords: - - "CS0417" -helpviewer_keywords: - - "CS0417" -ms.assetid: e2a617da-f0b2-4bad-aefa-3dd3bc1fb24b ---- -# Compiler Error CS0417 - -'identifier': cannot provide arguments when creating an instance of a variable type - - This error occurs if a call to the `new` operator on a type parameter has arguments. The only constructor that can be called by using the `new` operator on an unknown parameter type is a constructor that has no arguments. If you need to call another constructor, consider using a class type constraint or interface constraint. - -## Example - - The following example generates CS0417: - -```csharp -// CS0417 -class ExampleClass where T : new() -{ - // The following line causes CS0417. - T instance1 = new T(1); - - // The following line doesn't cause the error. - T instance2 = new T(); -} -``` - -## See also - -- [Constraints on Type Parameters](../../programming-guide/generics/constraints-on-type-parameters.md) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index 7baf635c5659a..cb55d95d677d0 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -2,10 +2,48 @@ title: Resolve errors and warnings related to generic type parameters and type arguments. description: These compiler errors and warnings indicate errors in generic type parameters and type arguments. f1_keywords: + - "CS0080" + - "CS0081" - "CS0304" + - "CS0305" + - "CS0306" + - "CS0307" + - "CS0308" + - "CS0310" + - "CS0311" + - "CS0312" + - "CS0313" + - "CS0314" + - "CS0315" + - "CS0403" + - "CS0412" + - "CS0413" + - "CS0417" + - "CS0694" + - "CS0695" + - "CS0698" - "CS9338" helpviewer_keywords: + - "CS0080" + - "CS0081" - "CS0304" + - "CS0305" + - "CS0306" + - "CS0307" + - "CS0308" + - "CS0310" + - "CS0311" + - "CS0312" + - "CS0313" + - "CS0314" + - "CS0315" + - "CS0403" + - "CS0412" + - "CS0413" + - "CS0694" + - "CS0695" + - "CS0698" + - "CS0417" - "CS9338" ms.date: 11/13/2025 ai-usage: ai-assisted @@ -17,9 +55,75 @@ This article covers the following compiler errors: +- [**CS0080**](#constraints-on-non-generic-declarations): *Constraints are not allowed on non-generic declarations* +- [**CS0081**](#type-parameter-declaration): *Type parameter declaration must be an identifier not a type* +- [**CS0224**](#vararg-method-restrictions): *A method with vararg cannot be generic, be in a generic type, or have a params parameter* - [**CS0304**](#new-constraint): *Cannot create an instance of the variable type 'type' because it does not have the new() constraint* +- [**CS0305**](#type-argument-count): *Using the generic type 'generic type' requires 'number' type arguments* +- [**CS0306**](#invalid-type-argument): *The type 'type' may not be used as a type argument* +- [**CS0307**](#non-generic-construct): *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* +- [**CS0308**](#non-generic-construct): *The non-generic type-or-method 'identifier' cannot be used with type arguments.* +- [**CS0310**](#new-constraint): *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'* +- [**CS0311**](#constraint-conversions): *The type 'type1' cannot be used as type parameter 'T' in the generic type or method '\'. There is no implicit reference conversion from 'type1' to 'type2'.* +- [**CS0312**](#nullable-type-constraints): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.* +- [**CS0314**](#constraint-conversions): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* +- [**CS0313**](#nullable-type-constraints): *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints.* +- [**CS0413**](#type-parameter-with-as-operator): *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint* +- [**CS0417**](#new-constraint): *'identifier': cannot provide arguments when creating an instance of a variable type* - [**CS0338**](#type-argument-visibility): *Inconsistent accessibility: type argument is less accessible than class.* +## Constraints on non-generic declarations + +The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](../fundamentals/types/generics.md). + +The following sample generates CS0080 because MyClass is not a generic class and Foo is not a generic method. + +```csharp +namespace MyNamespace +{ + public class MyClass where MyClass : System.IDisposable // CS0080 //the following line shows an example of correct syntax + //public class MyClass where T : System.IDisposable + { + public void Foo() where Foo : new() // CS0080 + //the following line shows an example of correct syntax + //public void Foo() where U : struct + { + } + } + + public class Program + { + public static void Main() + { + } + } +} +``` + +## Type parameter declaration + +When you declare a generic method or type, specify the type parameter as an identifier, for example "T" or "inputType". When client code calls the method, it supplies the type, which replaces each occurrence of the identifier in the method or class body. For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md). + +```csharp +// CS0081.cs +class MyClass +{ + public void F() {} // CS0081 + public void F(T input) {} // OK + + public static void Main() + { + MyClass a = new MyClass(); + a.F(2); + a.F(.05); + } +} +``` + +## Vararg method restrictions + +A method with vararg cannot be generic, be in a generic type, or have a params parameter. This restriction applies to methods that use the `__arglist` keyword for variable argument lists. + ## new() constraint When you implement a generic class, and you want to use the `new` keyword to create a new instance of any type that is supplied for a type parameter `T`, you must apply the [new() constraint](../keywords/new-constraint.md) to `T` in the class declaration, as shown in the following example. @@ -28,7 +132,7 @@ When you implement a generic class, and you want to use the `new` keyword to cre class C where T : new() ``` -The `new()` constraint enforces type safety by guaranteeing that any concrete type that is supplied for `T` has a parameterless constructor. CS0304 occurs if you attempt to use the `new` operator in the body of the class to create an instance of type parameter `T` when `T` does not specify the `new()` constraint. On the client side, if code attempts to instantiate the generic class with a type that has no parameterless constructor, that code will generate [Compiler Error CS0310](./cs0310.md). +The `new()` constraint enforces type safety by guaranteeing that any concrete type that is supplied for `T` has a parameterless constructor. CS0304 occurs if you attempt to use the `new` operator in the body of the class to create an instance of type parameter `T` when `T` does not specify the `new()` constraint. On the client side, if code attempts to instantiate the generic class with a type that has no parameterless constructor, that code will generate CS0310. The following example generates CS0304. @@ -71,6 +175,470 @@ class C where T : new() } ``` +The generic type or method defines the `new()` constraint in its `where` clause, so any type must have a public parameterless constructor in order to be used as a type argument for that generic type or method. To avoid this error, make sure that the type has the correct constructor, or modify the constraint clause of the generic type or method. + +The following sample generates CS0310: + +```csharp +// CS0310.cs +using System; + +class G where T : new() +{ + T t; + + public G() + { + t = new T(); + Console.WriteLine(t); + } +} + +class B +{ + private B() { } + // Try this instead: + // public B() { } +} + +class CMain +{ + public static void Main() + { + G g = new G(); // CS0310 + Console.WriteLine(g.ToString()); + } +} +``` + +This error occurs if a call to the `new` operator on a type parameter has arguments. The only constructor that can be called by using the `new` operator on an unknown parameter type is a constructor that has no arguments. If you need to call another constructor, consider using a class type constraint or interface constraint. + +The following example generates CS0417: + +```csharp +// CS0417 +class ExampleClass where T : new() +{ + // The following line causes CS0417. + T instance1 = new T(1); + + // The following line doesn't cause the error. + T instance2 = new T(); +} +``` + +## Type argument count + +This error occurs when the expected number of type arguments was not found. To resolve C0305, use the required number of type arguments. + +## Example + +The following sample generates CS0305. + +```csharp +// CS0305.cs +public class MyList {} +public class MyClass {} + +class MyClass +{ + public static void Main() + { + MyList list1 = new MyList(); // CS0305 + MyList list2 = new MyList(); // OK + } +} +``` + +## Invalid type argument + +The type used as a type parameter is not allowed. This could be because the type is a pointer type. + +The following example generates CS0306: + +```csharp +// CS0306.cs +class C +{ +} + +class M +{ + // CS0306 – int* not allowed as a type parameter + C f; +} +``` + +## Non-generic construct + +The construct named was not a type or a method, the only constructs that can take generic arguments. Remove the type arguments in angle brackets. If a generic is needed, declare your generic construct as a generic type or method. + +The method or type is not generic, but it was used with type arguments. To avoid this error, remove the angled brackets and type arguments, or redeclare the method or type as a generic method or type. + +The following sample generates CS0307: + +```csharp +// CS0307.cs +class C +{ + public int P { get { return 1; } } + public static void Main() + { + C c = new C(); + int p = c.P(); // CS0307 – C.P is a property + // Try this instead + // int p = c.P; + } +} +``` + +The following example generates CS0308: + +```csharp +// CS0308a.cs +class MyClass +{ + public void F() {} + public static void Main() + { + F(); // CS0308 – F is not generic. + // Try this instead: + // F(); + } +} +``` + +The following example also generates CS0308. To resolve the error, use the directive "using System.Collections.Generic." + +```csharp +// CS0308b.cs +// compile with: /t:library +using System.Collections; +// To resolve, uncomment the following line: +// using System.Collections.Generic; +public class MyStack +{ + // Store the elements of the stack: + private T[] items = new T[100]; + private int stack_counter = 0; + + // Define the iterator block: + public IEnumerator GetEnumerator() // CS0308 + { + for (int i = stack_counter - 1 ; i >= 0; i--) + yield return items[i]; + } +} +``` + +## Constraint conversions + +When a constraint is applied to a generic type parameter, an implicit identity or reference conversion must exist from the concrete argument to the type of the constraint. + +## To correct this error + +1. Change the type argument you are using to one that fulfills the constraint. + +2. If you own the class, you can remove the constraint or else do something to enable an implicit reference or identity conversion. For example, you can make the second type inherit from the first. + +## Example + +```csharp +// cs0311.cs +class B {} +class C {} +class Test where T : C +{ } + +class Program +{ + static void Main() + { + Test test = new Test(); //CS0311 + } +} +``` + +If this error occurs when trying to use a value-type argument, notice that an implicit numeric conversion, for example from `short` to `int`, does not satisfy a generic type parameter. + +When a generic type uses a type parameter that is constrained, the new class must also satisfy those same constraints. + +## To correct this error + +1. In the example that follows, add `where T : ClassConstraint` to class `B`. + +## Example + +The following code generates CS0314: + +```csharp +// cs0314.cs +// Compile with: /target:library +public class ClassConstraint { } + +public class A where T : ClassConstraint +{ } + +public class B : A //CS0314 +{ } + +// Try using this instead. +public class C : A where T : ClassConstraint +{ } +``` + +This error occurs when you constrain a generic type to a particular class, and try to construct an instance of that class by using a value type that cannot be implicitly boxed to it. + +## To correct this error + +1. One solution is to redefine the struct as a class. + +## Example + +The following example generates CS0315: + +```csharp +// cs0315.cs +public class ClassConstraint { } +public struct ViolateClassConstraint { } + +public class Gen where T : ClassConstraint +{ +} +public class Test +{ + public static int Main() + { + Gen g = new Gen(); //CS0315 + return 1; + } +} +``` + +## Nullable type constraints + +A nullable value type is distinct from its non-nullable counterpart; no implicit reference conversion or identify conversion exists between them. A nullable boxing conversion does not satisfy a generic type constraint. In the example that follows, the first type parameter is a `Nullable` and the second type parameter is a `System.Int32`. + +## To correct this error + +1. Remove the constraint. + +2. In the following example, make the second type argument either `int?` or `object`. + +## Example + +The following code generates CS0312: + +```csharp +// cs0312.cs +class Program +{ + static void MTyVar() where T : U { } + + static int Main() + { + MTyVar(); // CS0312 + return 1; + } +} +``` + +Although a nullable value type is distinct from a non-nullable type, various kinds of conversions are allowed between nullable and non-nullable values. + +A nullable value type is not equivalent to its non-nullable counterpart. In the example that follows, `ImplStruct` satisfies the `BaseInterface` constraint but `ImplStruct?` does not because `Nullable` does not implement `BaseInterface`. + +## To correct this error + +1. Using the code that follows as an example, one solution is to specify an ordinary `ImplStruct` as the first type argument in the call to `TestMethod`. Then modify `TestMethod` to create a nullable version of `Implstruct` in its return statement: + + ```csharp + return new Nullable(t); + ``` + +## Example + +The following code generates CS0313: + +```csharp +// cs0313.cs +public interface BaseInterface { } +public struct ImplStruct : BaseInterface { } + +public class TestClass +{ + public T? TestMethod(T t) where T : struct, U + { + return t; + } +} + +public class NullableTest +{ + public static void Run() + { + + TestClass tc = new TestClass(); + tc.TestMethod(new ImplStruct?()); // CS0313 + } + public static void Main() + { } +} +``` + +## Null assignment to type parameter + +You cannot assign null to the unknown type named because it might be a value type, which does not allow null assignment. If your generic class is not intended to accept value types, use the class type constraint. If it can accept value types, such as the built-in types, you may be able to replace the assignment to null with the expression `default(T)`, as shown in the following example. + +## Example + +The following sample generates CS0403. + +```csharp +// CS0403.cs +// compile with: /target:library +class C +{ + public void f() + { + T t = null; // CS0403 + T t2 = default(T); // OK + } +} + +class D where T : class +{ + public void f() + { + T t = null; // OK + } +} +``` + +## Type parameter name conflict + +There is a name conflict between the type parameter of a generic method and a local variable in the method or one of the method's parameters. To avoid this error, rename any conflicting parameters or local variables. + +## Example + +The following sample generates CS0412: + +```csharp +// CS0412.cs +using System; + +class C +{ + // Parameter name is the same as method type parameter name + public void G(int T) // CS0412 + { + } + public void F() + { + // Method local variable name is the same as method type + // parameter name + double T = 0.0; // CS0412 + Console.WriteLine(T); + } + + public static void Main() + { + } +} +``` + +## Type parameter with as operator + +This error occurs if a generic type uses the [as](../operators/type-testing-and-cast.md#the-as-operator) operator, but that generic type does not have a class type constraint. The `as` operator is only allowed with reference and nullable value types, so the type parameter must be constrained to guarantee that it is not a value type. To avoid this error, use a class type constraint or a reference type constraint. + +This is because the `as` operator could return `null`, which is not a possible value for a value type, and the type parameter must be treated as a value type unless it is a class type constraint or a reference type constraint. + +## Example + +The following sample generates CS0413. + +```csharp +// CS0413.cs +// compile with: /target:library +class A {} +class B : A {} + +class CMain +{ + A a = null; + public void G() + { + a = new A(); + System.Console.WriteLine (a as T); // CS0413 + } + + // OK + public void H() where T : A + { + a = new A(); + System.Console.WriteLine (a as T); + } +} +``` + +## Type parameter same name as containing type or method + +You must use a different name for the type parameter since the type parameter's name cannot be identical to the type or method name that contains the type parameter. + +### Example 1 + +The following sample generates CS0694. + +```csharp +// CS0694.cs +// compile with: /target:library +class C {} // CS0694 +``` + +### Example 2 + +In addition to the above case involving a generic class, this error may occur with a method: + +```csharp +// CS0694_2.cs +// compile with: /target:library +class A +{ + public void F(F arg); // CS0694 +} +``` + +## Cannot implement same generic interface with different type parameters + +This error occurs when a generic class implements more than one parameterization of the same generic interface, and there exists a type parameter substitution which would make the two interfaces identical. To avoid this error, implement only one of the interfaces, or change the type parameters to avoid the conflict. + +The following sample generates CS0695: + +```csharp +// CS0695.cs +// compile with: /target:library + +interface I +{ +} + +class G : I, I // CS0695 +{ +} +``` + +## Generic type cannot derive from attribute + +Any class that derives from an attribute class is an attribute. Attributes are not allowed to be generic types. + +The following sample generates CS0698: + +```csharp +// CS0698.cs +class C : System.Attribute // CS0698 +{ +} +``` + ## Type argument visibility The following errors relate to accessibilty of type arguments: diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index bb7c86b4c1539..f762872afb067 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -512,8 +512,10 @@ items: - name: Generic type parameters and type arguments href: ./compiler-messages/generic-type-parameters-errors.md displayName: > - generic, type parameter, type argument, constraint, - CS0304, CS9338 + generic, type parameter, type argument, constraint, + CS0080, CS0081, CS0224, CS0304, CS0305, CS0306, CS0307, CS0308, CS0310, CS0311, + CS0312, CS0313, CS0314, CS0315, CS0403, CS0412, CS0413, CS0417, CS0694, CS0695, + CS0698, CS9338 - name: asynchronous methods href: ./compiler-messages/async-await-errors.md displayName: > @@ -778,10 +780,6 @@ items: href: ../misc/cs0077.md - name: CS0079 href: ../misc/cs0079.md - - name: CS0080 - href: ../misc/cs0080.md - - name: CS0081 - href: ../misc/cs0081.md - name: CS0082 href: ../misc/cs0082.md - name: CS0100 @@ -968,32 +966,10 @@ items: href: ../misc/cs0281.md - name: CS0283 href: ../misc/cs0283.md - - name: CS0305 - href: ../misc/cs0305.md - - name: CS0306 - href: ../misc/cs0306.md - - name: CS0307 - href: ../misc/cs0307.md - - name: CS0308 - href: ../misc/cs0308.md - - name: CS0310 - href: ./compiler-messages/cs0310.md - - name: CS0311 - href: ./compiler-messages/cs0311.md - - name: CS0312 - href: ../misc/cs0312.md - - name: CS0313 - href: ../misc/cs0313.md - - name: CS0314 - href: ../misc/cs0314.md - - name: CS0315 - href: ../misc/cs0315.md - name: CS0316 href: ../misc/cs0316.md - name: CS0401 href: ../misc/cs0401.md - - name: CS0403 - href: ../misc/cs0403.md - name: CS0405 href: ../misc/cs0405.md - name: CS0406 @@ -1008,10 +984,6 @@ items: href: ../misc/cs0411.md - name: CS0412 href: ../misc/cs0412.md - - name: CS0413 - href: ./compiler-messages/cs0413.md - - name: CS0417 - href: ./compiler-messages/cs0417.md - name: CS0418 href: ../misc/cs0418.md - name: CS0423 diff --git a/docs/csharp/misc/cs0080.md b/docs/csharp/misc/cs0080.md deleted file mode 100644 index 9a589b5453da8..0000000000000 --- a/docs/csharp/misc/cs0080.md +++ /dev/null @@ -1,39 +0,0 @@ ---- -description: "Compiler Error CS0080" -title: "Compiler Error CS0080" -ms.date: 07/20/2015 -f1_keywords: - - "CS0080" -helpviewer_keywords: - - "CS0080" -ms.assetid: 99035371-37d1-48b2-a8b9-e8a1ebd04f0f ---- -# Compiler Error CS0080 - -Constraints are not allowed on non-generic declarations - - The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](../fundamentals/types/generics.md). - - The following sample generates CS0080 because MyClass is not a generic class and Foo is not a generic method. - -```csharp -namespace MyNamespace -{ - public class MyClass where MyClass : System.IDisposable // CS0080 //the following line shows an example of correct syntax - //public class MyClass where T : System.IDisposable - { - public void Foo() where Foo : new() // CS0080 - //the following line shows an example of correct syntax - //public void Foo() where U : struct - { - } - } - - public class Program - { - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0081.md b/docs/csharp/misc/cs0081.md deleted file mode 100644 index 4e6e0a95b6da5..0000000000000 --- a/docs/csharp/misc/cs0081.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS0081" -title: "Compiler Error CS0081" -ms.date: 07/20/2015 -f1_keywords: - - "CS0081" -helpviewer_keywords: - - "CS0081" -ms.assetid: a5649abc-89ea-4f64-8c3c-eb36df926561 ---- -# Compiler Error CS0081 - -Type parameter declaration must be an identifier not a type - - When you declare a generic method or type, specify the type parameter as an identifier, for example "T" or "inputType". When client code calls the method, it supplies the type, which replaces each occurrence of the identifier in the method or class body. For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md). - -```csharp -// CS0081.cs -class MyClass -{ - public void F() {} // CS0081 - public void F(T input) {} // OK - - public static void Main() - { - MyClass a = new MyClass(); - a.F(2); - a.F(.05); - } -} -``` - -## See also - -- [Generics](../fundamentals/types/generics.md) diff --git a/docs/csharp/misc/cs0305.md b/docs/csharp/misc/cs0305.md deleted file mode 100644 index d1c6bae538dc4..0000000000000 --- a/docs/csharp/misc/cs0305.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS0305" -title: "Compiler Error CS0305" -ms.date: 07/20/2015 -f1_keywords: - - "CS0305" -helpviewer_keywords: - - "CS0305" -ms.assetid: a862c484-01fe-4067-b0f4-15a618e7f8a1 ---- -# Compiler Error CS0305 - -Using the generic type 'generic type' requires 'number' type arguments - - This error occurs when the expected number of type arguments was not found. To resolve C0305, use the required number of type arguments. - -## Example - - The following sample generates CS0305. - -```csharp -// CS0305.cs -public class MyList {} -public class MyClass {} - -class MyClass -{ - public static void Main() - { - MyList list1 = new MyList(); // CS0305 - MyList list2 = new MyList(); // OK - } -} -``` diff --git a/docs/csharp/misc/cs0306.md b/docs/csharp/misc/cs0306.md deleted file mode 100644 index c48dad087df35..0000000000000 --- a/docs/csharp/misc/cs0306.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -description: "Compiler Error CS0306" -title: "Compiler Error CS0306" -ms.date: 07/20/2015 -f1_keywords: - - "CS0306" -helpviewer_keywords: - - "CS0306" -ms.assetid: f340a3ce-6140-4001-bb00-628a2985ddd6 ---- -# Compiler Error CS0306 - -The type 'type' may not be used as a type argument - - The type used as a type parameter is not allowed. This could be because the type is a pointer type. - - The following example generates CS0306: - -```csharp -// CS0306.cs -class C -{ -} - -class M -{ - // CS0306 – int* not allowed as a type parameter - C f; -} -``` diff --git a/docs/csharp/misc/cs0307.md b/docs/csharp/misc/cs0307.md deleted file mode 100644 index 444b8568fbcf3..0000000000000 --- a/docs/csharp/misc/cs0307.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS0307" -title: "Compiler Error CS0307" -ms.date: 07/20/2015 -f1_keywords: - - "CS0307" -helpviewer_keywords: - - "CS0307" -ms.assetid: 202a9985-ed7a-4e0a-9573-5624e066d314 ---- -# Compiler Error CS0307 - -The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression. - - The construct named was not a type or a method, the only constructs that can take generic arguments. Remove the type arguments in angle brackets. If a generic is needed, declare your generic construct as a generic type or method. - - The following sample generates CS0307: - -```csharp -// CS0307.cs -class C -{ - public int P { get { return 1; } } - public static void Main() - { - C c = new C(); - int p = c.P(); // CS0307 – C.P is a property - // Try this instead - // int p = c.P; - } -} -``` diff --git a/docs/csharp/misc/cs0308.md b/docs/csharp/misc/cs0308.md deleted file mode 100644 index 077da8f1c52d3..0000000000000 --- a/docs/csharp/misc/cs0308.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -description: "Compiler Error CS0308" -title: "Compiler Error CS0308" -ms.date: 07/20/2015 -f1_keywords: - - "CS0308" -helpviewer_keywords: - - "CS0308" -ms.assetid: b52ef9d2-f5b3-4baf-9a7e-bb1371e79463 ---- -# Compiler Error CS0308 - -The non-generic type-or-method 'identifier' cannot be used with type arguments. - - The method or type is not generic, but it was used with type arguments. To avoid this error, remove the angled brackets and type arguments, or redeclare the method or type as a generic method or type. - - The following example generates CS0308: - -```csharp -// CS0308a.cs -class MyClass -{ - public void F() {} - public static void Main() - { - F(); // CS0308 – F is not generic. - // Try this instead: - // F(); - } -} -``` - - The following example also generates CS0308. To resolve the error, use the directive "using System.Collections.Generic." - -```csharp -// CS0308b.cs -// compile with: /t:library -using System.Collections; -// To resolve, uncomment the following line: -// using System.Collections.Generic; -public class MyStack -{ - // Store the elements of the stack: - private T[] items = new T[100]; - private int stack_counter = 0; - - // Define the iterator block: - public IEnumerator GetEnumerator() // CS0308 - { - for (int i = stack_counter - 1 ; i >= 0; i--) - yield return items[i]; - } -} -``` diff --git a/docs/csharp/misc/cs0312.md b/docs/csharp/misc/cs0312.md deleted file mode 100644 index 7fa791b1fb1aa..0000000000000 --- a/docs/csharp/misc/cs0312.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -description: "Compiler Error CS0312" -title: "Compiler Error CS0312" -ms.date: 07/20/2015 -f1_keywords: - - "CS0312" -helpviewer_keywords: - - "CS0312" -ms.assetid: 552db0ae-2ecf-4beb-9606-bbe58e5708f6 ---- -# Compiler Error CS0312 - -The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'. - - A nullable value type is distinct from its non-nullable counterpart; no implicit reference conversion or identify conversion exists between them. A nullable boxing conversion does not satisfy a generic type constraint. In the example that follows, the first type parameter is a `Nullable` and the second type parameter is a `System.Int32`. - -## To correct this error - -1. Remove the constraint. - -2. In the following example, make the second type argument either `int?` or `object`. - -## Example - -The following code generates CS0312: - -```csharp -// cs0312.cs -class Program -{ - static void MTyVar() where T : U { } - - static int Main() - { - MTyVar(); // CS0312 - return 1; - } -} -``` - - Although a nullable value type is distinct from a non-nullable type, various kinds of conversions are allowed between nullable and non-nullable values. - -## See also - -- [Nullable value types](../language-reference/builtin-types/nullable-value-types.md) diff --git a/docs/csharp/misc/cs0313.md b/docs/csharp/misc/cs0313.md deleted file mode 100644 index ecf48a04b432c..0000000000000 --- a/docs/csharp/misc/cs0313.md +++ /dev/null @@ -1,57 +0,0 @@ ---- -description: "Compiler Error CS0313" -title: "Compiler Error CS0313" -ms.date: 07/20/2015 -f1_keywords: - - "CS0313" -helpviewer_keywords: - - "CS0313" -ms.assetid: a0b0f2fb-e742-4df8-98bd-3bc068f0c71c ---- -# Compiler Error CS0313 - -The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints. - - A nullable value type is not equivalent to its non-nullable counterpart. In the example that follows, `ImplStruct` satisfies the `BaseInterface` constraint but `ImplStruct?` does not because `Nullable` does not implement `BaseInterface`. - -## To correct this error - -1. Using the code that follows as an example, one solution is to specify an ordinary `ImplStruct` as the first type argument in the call to `TestMethod`. Then modify `TestMethod` to create a nullable version of `Implstruct` in its return statement: - - ```csharp - return new Nullable(t); - ``` - -## Example - -The following code generates CS0313: - -```csharp -// cs0313.cs -public interface BaseInterface { } -public struct ImplStruct : BaseInterface { } - -public class TestClass -{ - public T? TestMethod(T t) where T : struct, U - { - return t; - } -} - -public class NullableTest -{ - public static void Run() - { - - TestClass tc = new TestClass(); - tc.TestMethod(new ImplStruct?()); // CS0313 - } - public static void Main() - { } -} -``` - -## See also - -- [Nullable value types](../language-reference/builtin-types/nullable-value-types.md) diff --git a/docs/csharp/misc/cs0314.md b/docs/csharp/misc/cs0314.md deleted file mode 100644 index 5e24204983c03..0000000000000 --- a/docs/csharp/misc/cs0314.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0314" -title: "Compiler Error CS0314" -ms.date: 07/20/2015 -f1_keywords: - - "CS0314" -helpviewer_keywords: - - "CS0314" -ms.assetid: 12f68f51-0568-4e80-b0fd-15899807477d ---- -# Compiler Error CS0314 - -The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'. - - When a generic type uses a type parameter that is constrained, the new class must also satisfy those same constraints. - -## To correct this error - -1. In the example that follows, add `where T : ClassConstraint` to class `B`. - -## Example - - The following code generates CS0314: - -```csharp -// cs0314.cs -// Compile with: /target:library -public class ClassConstraint { } - -public class A where T : ClassConstraint -{ } - -public class B : A //CS0314 -{ } - -// Try using this instead. -public class C : A where T : ClassConstraint -{ } -``` - -## See also - -- [Constraints on Type Parameters](../programming-guide/generics/constraints-on-type-parameters.md) diff --git a/docs/csharp/misc/cs0315.md b/docs/csharp/misc/cs0315.md deleted file mode 100644 index 772c398ba60a4..0000000000000 --- a/docs/csharp/misc/cs0315.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS0315" -title: "Compiler Error CS0315" -ms.date: 07/20/2015 -f1_keywords: - - "CS0315" -helpviewer_keywords: - - "CS0315" -ms.assetid: 9bb1cab3-1dca-4467-978b-1ab310901a70 ---- -# Compiler Error CS0315 - -The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\'. There is no boxing conversion from 'valueType' to 'referenceType'. - - This error occurs when you constrain a generic type to a particular class, and try to construct an instance of that class by using a value type that cannot be implicitly boxed to it. - -## To correct this error - -1. One solution is to redefine the struct as a class. - -## Example - - The following example generates CS0315: - -```csharp -// cs0315.cs -public class ClassConstraint { } -public struct ViolateClassConstraint { } - -public class Gen where T : ClassConstraint -{ -} -public class Test -{ - public static int Main() - { - Gen g = new Gen(); //CS0315 - return 1; - } -} -``` - -## See also - -- [Constraints on Type Parameters](../programming-guide/generics/constraints-on-type-parameters.md) -- [Boxing and Unboxing](../programming-guide/types/boxing-and-unboxing.md) diff --git a/docs/csharp/misc/cs0403.md b/docs/csharp/misc/cs0403.md deleted file mode 100644 index 6a0f3a4729eb9..0000000000000 --- a/docs/csharp/misc/cs0403.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0403" -title: "Compiler Error CS0403" -ms.date: 07/20/2015 -f1_keywords: - - "CS0403" -helpviewer_keywords: - - "CS0403" -ms.assetid: 6e5d55ce-d6bf-419d-aded-aaa2e5963bb6 ---- -# Compiler Error CS0403 - -Cannot convert null to type parameter 'name' because it could be a non-nullable value type. Consider using default('T') instead. - - You cannot assign null to the unknown type named because it might be a value type, which does not allow null assignment. If your generic class is not intended to accept value types, use the class type constraint. If it can accept value types, such as the built-in types, you may be able to replace the assignment to null with the expression `default(T)`, as shown in the following example. - -## Example - - The following sample generates CS0403. - -```csharp -// CS0403.cs -// compile with: /target:library -class C -{ - public void f() - { - T t = null; // CS0403 - T t2 = default(T); // OK - } -} - -class D where T : class -{ - public void f() - { - T t = null; // OK - } -} -``` diff --git a/docs/csharp/misc/cs0412.md b/docs/csharp/misc/cs0412.md deleted file mode 100644 index 1bab6319cb554..0000000000000 --- a/docs/csharp/misc/cs0412.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0412" -title: "Compiler Error CS0412" -ms.date: 07/20/2015 -f1_keywords: - - "CS0412" -helpviewer_keywords: - - "CS0412" -ms.assetid: eeb2afbc-9416-4bcf-b116-d6adc5cfd4ca ---- -# Compiler Error CS0412 - -'generic': a parameter or local variable cannot have the same name as a method type parameter - - There is a name conflict between the type parameter of a generic method and a local variable in the method or one of the method's parameters. To avoid this error, rename any conflicting parameters or local variables. - -## Example - - The following sample generates CS0412: - -```csharp -// CS0412.cs -using System; - -class C -{ - // Parameter name is the same as method type parameter name - public void G(int T) // CS0412 - { - } - public void F() - { - // Method local variable name is the same as method type - // parameter name - double T = 0.0; // CS0412 - Console.WriteLine(T); - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0694.md b/docs/csharp/misc/cs0694.md deleted file mode 100644 index 85c30e1a372b3..0000000000000 --- a/docs/csharp/misc/cs0694.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -description: "Compiler Error CS0694" -title: "Compiler Error CS0694" -ms.date: 07/20/2015 -f1_keywords: - - "CS0694" -helpviewer_keywords: - - "CS0694" -ms.assetid: 048615e4-4599-4726-b5db-55322ccc936f ---- -# Compiler Error CS0694 - -Type parameter 'identifier' has the same name as the containing type, or method - - You must use a different name for the type parameter since the type parameter's name cannot be identical to the type or method name that contains the type parameter. - -## Example 1 - - The following sample generates CS0694. - -```csharp -// CS0694.cs -// compile with: /target:library -class C {} // CS0694 -``` - -## Example 2 - - In addition to the above case involving a generic class, this error may occur with a method: - -```csharp -// CS0694_2.cs -// compile with: /target:library -class A -{ - public void F(F arg); // CS0694 -} -``` diff --git a/docs/csharp/misc/cs0695.md b/docs/csharp/misc/cs0695.md deleted file mode 100644 index b81ee2dc70ab1..0000000000000 --- a/docs/csharp/misc/cs0695.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -description: "Compiler Error CS0695" -title: "Compiler Error CS0695" -ms.date: 07/20/2015 -f1_keywords: - - "CS0695" -helpviewer_keywords: - - "CS0695" -ms.assetid: 05f6c8cf-6147-4ac7-84ea-e1f34f8ef9f7 ---- -# Compiler Error CS0695 - -'generic type' cannot implement both 'generic interface' and 'generic interface' because they may unify for some type parameter substitutions - - This error occurs when a generic class implements more than one parameterization of the same generic interface, and there exists a type parameter substitution which would make the two interfaces identical. To avoid this error, implement only one of the interfaces, or change the type parameters to avoid the conflict. - - The following sample generates CS0695: - -```csharp -// CS0695.cs -// compile with: /target:library - -interface I -{ -} - -class G : I, I // CS0695 -{ -} -``` diff --git a/docs/csharp/misc/cs0698.md b/docs/csharp/misc/cs0698.md deleted file mode 100644 index 29664d4e8b13d..0000000000000 --- a/docs/csharp/misc/cs0698.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -description: "Compiler Error CS0698" -title: "Compiler Error CS0698" -ms.date: 07/20/2015 -f1_keywords: - - "CS0698" -helpviewer_keywords: - - "CS0698" -ms.assetid: 68211652-fdfa-4d37-9451-f0b4238f9fe6 ---- -# Compiler Error CS0698 - -A generic type cannot derive from 'class' because it is an attribute class - - Any class that derives from an attribute class is an attribute. Attributes are not allowed to be generic types. - - The following sample generates CS0698: - -```csharp -// CS0698.cs -class C : System.Attribute // CS0698 -{ -} -``` diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index bbb3152f2f810..715a1ec10f917 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -4,7 +4,6 @@ description: "List of possible resources for compiler errors and warnings that h ms.date: 05/23/2025 f1_keywords: - "CS0190" - - "CS0224" - "CS0257" - "CS0595" - "CS0847" @@ -536,6 +535,786 @@ f1_keywords: # C# 14 errors begin here - "CS9338" helpviewer_keywords: + - "CS0190" + - "CS0257" + - "CS0595" + - "CS0847" + - "CS0856" + - "CS0857" + - "CS1066" + - "CS1072" + - "CS1073" + - "CS1636" + - "CS1669" + - "CS1734" + - "CS1735" + - "CS1745" + - "CS1747" + - "CS1748" + - "CS1752" + - "CS1754" + - "CS1756" + - "CS1757" + - "CS1758" + - "CS1759" + - "CS1761" + - "CS1764" + - "CS1766" + - "CS1767" + - "CS1768" + - "CS1769" + - "CS1770" + - "CS1773" + - "CS1774" + - "CS1960" + - "CS1961" + - "CS1982" + - "CS1996" + - "CS1997" + - "CS2038" + - "CS2039" + - "CS2040" + - "CS2041" + - "CS2042" + - "CS2043" + - "CS2044" + - "CS2045" + - "CS2046" + - "CS3028" + - "CS4001" + - "CS4003" + - "CS4005" + - "CS4006" + - "CS4007" + - "CS4010" + - "CS4011" + - "CS4012" + - "CS4015" + - "CS4016" + - "CS4017" + - "CS4018" + - "CS4019" + - "CS4020" + - "CS4021" + - "CS4022" + - "CS4023" + - "CS4024" + - "CS4025" + - "CS4026" + - "CS4027" + - "CS4028" + - "CS4029" + - "CS4030" + - "CS4031" + - "CS4034" + - "CS4036" + - "CS7002" + - "CS7006" + - "CS7012" + - "CS7013" + - "CS7015" + - "CS7016" + - "CS7017" + - "CS7018" + - "CS7019" + - "CS7020" + - "CS7021" + - "CS7022" + - "CS7024" + - "CS7025" + - "CS7026" + - "CS7027" + - "CS7028" + - "CS7029" + - "CS7030" + - "CS7032" + - "CS7033" + - "CS7034" + - "CS7035" + - "CS7038" + - "CS7041" + - "CS7042" + - "CS7043" + - "CS7045" + - "CS7048" + - "CS7049" + - "CS7050" + - "CS7051" + - "CS7052" + - "CS7054" + - "CS7055" + - "CS7056" + - "CS7057" + - "CS7058" + - "CS7059" + - "CS7061" + - "CS7064" + - "CS7065" + - "CS7066" + - "CS7070" + - "CS7080" + - "CS7081" + - "CS7082" + - "CS7084" + - "CS7086" + - "CS7087" + - "CS7088" + - "CS7089" + - "CS7090" + - "CS7091" + - "CS7092" + - "CS7093" + - "CS7094" + - "CS7095" + - "CS7096" + - "CS7098" + - "CS7099" + - "CS7100" + - "CS7101" + - "CS7102" + - "CS7103" + - "CS8001" + - "CS8002" + - "CS8003" + - "CS8004" + - "CS8005" + - "CS8006" + - "CS8007" + - "CS8008" + - "CS8009" + - "CS8010" + - "CS8011" + - "CS8012" + - "CS8013" + - "CS8014" + - "CS8015" + - "CS8016" + - "CS8017" + - "CS8018" + - "CS8020" + - "CS8021" + - "CS8027" + - "CS8028" + - "CS8029" + - "CS8030" + - "CS8031" + - "CS8032" + - "CS8033" + - "CS8034" + - "CS8035" + - "CS8036" + - "CS8040" + - "CS8050" + - "CS8051" + - "CS8053" + - "CS8055" + - "CS8057" + - "CS8058" + - "CS8070" + - "CS8076" + - "CS8077" + - "CS8078" + - "CS8079" + - "CS8080" + - "CS8081" + - "CS8082" + - "CS8084" + - "CS8086" + - "CS8087" + - "CS8088" + - "CS8089" + - "CS8092" + - "CS8093" + - "CS8094" + - "CS8095" + - "CS8096" + - "CS8099" + - "CS8100" + - "CS8101" + - "CS8106" + - "CS8107" + - "CS8108" + - "CS8109" + - "CS8110" + - "CS8111" + - "CS8112" + - "CS8113" + - "CS8114" + - "CS8115" + - "CS8116" + - "CS8117" + - "CS8118" + - "CS8119" + - "CS8120" + - "CS8121" + - "CS8122" + - "CS8123" + - "CS8124" + - "CS8125" + - "CS8126" + - "CS8127" + - "CS8128" + - "CS8129" + - "CS8130" + - "CS8131" + - "CS8132" + - "CS8133" + - "CS8134" + - "CS8135" + - "CS8136" + - "CS8137" + - "CS8138" + - "CS8139" + - "CS8140" + - "CS8141" + - "CS8142" + - "CS8143" + - "CS8144" + - "CS8145" + - "CS8146" + - "CS8147" + - "CS8148" + - "CS8149" + - "CS8150" + - "CS8151" + - "CS8153" + - "CS8154" + - "CS8155" + - "CS8156" + - "CS8157" + - "CS8158" + - "CS8159" + - "CS8160" + - "CS8161" + - "CS8162" + - "CS8163" + - "CS8164" + - "CS8165" + - "CS8166" + - "CS8167" + - "CS8168" + - "CS8169" + - "CS8170" + - "CS8171" + - "CS8172" + - "CS8173" + - "CS8174" + - "CS8175" + - "CS8176" + - "CS8177" + - "CS8178" + - "CS8179" + - "CS8180" + - "CS8181" + - "CS8182" + - "CS8183" + - "CS8184" + - "CS8185" + - "CS8186" + - "CS8187" + - "CS8188" + - "CS8189" + - "CS8190" + - "CS8191" + - "CS8192" + - "CS8193" + - "CS8194" + - "CS8195" + - "CS8196" + - "CS8197" + - "CS8198" + - "CS8199" + - "CS8200" + - "CS8201" + - "CS8202" + - "CS8203" + - "CS8204" + - "CS8205" + - "CS8206" + - "CS8207" + - "CS8208" + - "CS8209" + - "CS8210" + - "CS8211" + - "CS8212" + - "CS8213" + - "CS8214" + - "CS8215" + - "CS8216" + - "CS8217" + - "CS8218" + - "CS8219" + - "CS8220" + - "CS8221" + - "CS8222" + - "CS8223" + - "CS8224" + - "CS8225" + - "CS8226" + - "CS8227" + - "CS8228" + - "CS8229" + - "CS8230" + - "CS8231" + - "CS8232" + - "CS8233" + - "CS8234" + - "CS8235" + - "CS8236" + - "CS8237" + - "CS8238" + - "CS8239" + - "CS8240" + - "CS8241" + - "CS8242" + - "CS8243" + - "CS8244" + - "CS8245" + - "CS8246" + - "CS8247" + - "CS8248" + - "CS8249" + - "CS8250" + - "CS8251" + - "CS8252" + - "CS8253" + - "CS8254" + - "CS8255" + - "CS8256" + - "CS8257" + - "CS8258" + - "CS8259" + - "CS8260" + - "CS8261" + - "CS8262" + - "CS8263" + - "CS8264" + - "CS8265" + - "CS8266" + - "CS8267" + - "CS8268" + - "CS8269" + - "CS8270" + - "CS8271" + - "CS8272" + - "CS8273" + - "CS8274" + - "CS8275" + - "CS8276" + - "CS8277" + - "CS8278" + - "CS8279" + - "CS8280" + - "CS8281" + - "CS8282" + - "CS8283" + - "CS8284" + - "CS8285" + - "CS8286" + - "CS8287" + - "CS8288" + - "CS8289" + - "CS8290" + - "CS8291" + - "CS8292" + - "CS8293" + - "CS8294" + - "CS8295" + - "CS8296" + - "CS8297" + - "CS8298" + - "CS8299" + - "CS8300" + - "CS8301" + - "CS8302" + - "CS8303" + - "CS8304" + - "CS8305" + - "CS8306" + - "CS8307" + - "CS8308" + - "CS8309" + - "CS8310" + - "CS8311" + - "CS8312" + - "CS8313" + - "CS8314" + - "CS8315" + - "CS8316" + - "CS8317" + - "CS8318" + - "CS8319" + - "CS8320" + - "CS8321" + - "CS8322" + - "CS8323" + - "CS8324" + - "CS8325" + - "CS8326" + - "CS8327" + - "CS8328" + - "CS8329" + - "CS8330" + - "CS8331" + - "CS8332" + - "CS8333" + - "CS8334" + - "CS8335" + - "CS8336" + - "CS8337" + - "CS8338" + - "CS8339" + - "CS8340" + - "CS8341" + - "CS8342" + - "CS8343" + - "CS8344" + - "CS8345" + - "CS8346" + - "CS8347" + - "CS8348" + - "CS8349" + - "CS8350" + - "CS8351" + - "CS8352" + - "CS8353" + - "CS8354" + - "CS8355" + - "CS8356" + - "CS8357" + - "CS8358" + - "CS8359" + - "CS8360" + - "CS8361" + - "CS8362" + - "CS8363" + - "CS8364" + - "CS8365" + - "CS8366" + - "CS8367" + - "CS8368" + - "CS8369" + - "CS8370" + - "CS8371" + - "CS8372" + - "CS8373" + - "CS8374" + - "CS8375" + - "CS8376" + - "CS8377" + - "CS8378" + - "CS8379" + - "CS8380" + - "CS8381" + - "CS8382" + - "CS8383" + - "CS8384" + - "CS8385" + - "CS8386" + - "CS8387" + - "CS8388" + - "CS8389" + - "CS8390" + - "CS8391" + - "CS8392" + - "CS8393" + - "CS8394" + - "CS8395" + - "CS8396" + - "CS8397" + - "CS8398" + - "CS8399" + - "CS8400" + - "CS8401" + - "CS8402" + - "CS8403" + - "CS8404" + - "CS8405" + - "CS8406" + - "CS8407" + - "CS8408" + - "CS8409" + - "CS8410" + - "CS8411" + - "CS8412" + - "CS8413" + - "CS8414" + - "CS8415" + - "CS8416" + - "CS8417" + - "CS8418" + - "CS8419" + - "CS8420" + - "CS8421" + - "CS8422" + - "CS8423" + - "CS8424" + - "CS8425" + - "CS8426" + - "CS8427" + - "CS8428" + - "CS8429" + - "CS8502" + - "CS8503" + - "CS8504" + - "CS8505" + - "CS8506" + - "CS8508" + - "CS8510" + - "CS8512" + - "CS8513" + - "CS8516" + - "CS8517" + - "CS8518" + - "CS8519" + - "CS8520" + - "CS8521" + - "CS8522" + - "CS8523" + - "CS8524" + - "CS8635" + - "CS8641" + - "CS8646" + - "CS8650" + - "CS8651" + - "CS8656" + - "CS8657" + - "CS8658" + - "CS8659" + - "CS8660" + - "CS8661" + - "CS8662" + - "CS8664" + - "CS8665" + - "CS8666" + - "CS8669" + - "CS8700" + - "CS8701" + - "CS8702" + - "CS8707" + - "CS8712" + - "CS8715" + - "CS8716" + - "CS8750" + - "CS8751" + - "CS8752" + - "CS8753" + - "CS8754" + - "CS8755" + - "CS8756" + - "CS8757" + - "CS8758" + - "CS8759" + - "CS8760" + - "CS8761" + - "CS8762" + - "CS8763" + - "CS8764" + - "CS8765" + - "CS8766" + - "CS8800" + - "CS8801" + - "CS8802" + - "CS8803" + - "CS8804" + - "CS8805" + - "CS8806" + - "CS8900" + - "CS8901" + - "CS8902" + - "CS8903" + - "CS8904" + - "CS8905" + - "CS8906" + - "CS8907" + - "CS8908" + - "CS8909" + - "CS8910" + - "CS8911" + - "CS8912" + - "CS8913" + - "CS8914" + - "CS8915" + - "CS8916" + - "CS8917" + - "CS8918" + - "CS8919" + - "CS8920" + - "CS8921" + - "CS8922" + - "CS8923" + - "CS8924" + - "CS8925" + - "CS8926" + - "CS8927" + - "CS8928" + - "CS8929" + - "CS8930" + - "CS8931" + - "CS8932" + - "CS8933" + - "CS8934" + - "CS8935" + - "CS8936" + - "CS8937" + - "CS8938" + - "CS8939" + - "CS8940" + - "CS8941" + - "CS8942" + - "CS8943" + - "CS8944" + - "CS8945" + - "CS8946" + - "CS8947" + - "CS8948" + - "CS8949" + - "CS8950" + - "CS8951" + - "CS8952" + - "CS8953" + - "CS8955" + - "CS8956" + - "CS8957" + - "CS8958" + - "CS8959" + - "CS8960" + - "CS8961" + - "CS8962" + - "CS8963" + - "CS8964" + - "CS8965" + - "CS8966" + - "CS8967" + - "CS8968" + - "CS8969" + - "CS8970" + - "CS8971" + - "CS8972" + - "CS8973" + - "CS8974" + - "CS8975" + - "CS8976" + - "CS8977" + - "CS8978" + - "CS8979" + - "CS8980" + - "CS8981" + - "CS8982" + - "CS8983" + - "CS8984" + - "CS8985" + - "CS8986" + - "CS8987" + - "CS8988" + - "CS8989" + - "CS8990" + - "CS8991" + - "CS8992" + - "CS8993" + - "CS8994" + - "CS8995" + - "CS8996" + - "CS8997" + - "CS8998" + - "CS8999" + - "CS9000" + - "CS9001" + - "CS9002" + - "CS9003" + - "CS9004" + - "CS9005" + - "CS9006" + - "CS9007" + - "CS9008" + - "CS9009" + - "CS9010" + - "CS9011" + - "CS9012" + - "CS9013" + - "CS9014" + - "CS9015" + - "CS9016" + - "CS9017" + - "CS9018" + - "CS9019" + - "CS9020" + - "CS9021" + - "CS9022" + - "CS9023" + - "CS9024" + - "CS9025" + - "CS9026" + - "CS9027" + - "CS9028" + - "CS9029" + - "CS9030" + - "CS9031" + - "CS9032" + - "CS9033" + - "CS9034" + - "CS9035" + - "CS9036" + - "CS9037" + - "CS9038" + - "CS9039" + - "CS9040" + - "CS9041" + - "CS9042" + - "CS9043" + - "CS9044" + - "CS9045" + - "CS9046" + - "CS9047" + - "CS9048" + - "CS9049" + - "CS9050" + - "CS9051" + - "CS9052" + - "CS9053" + - "CS9054" + - "CS9055" + - "CS9056" + - "CS9057" + - "CS9058" + - "CS9059" + - "CS9060" + - "CS9061" + - "CS9062" + - "CS9063" + - "CS9065" + - "CS9066" + - "CS9067" + - "CS9068" + - "CS9069" + - "CS9070" + - "CS9071" + - "CS9073" + - "CS9074" + - "CS9075" + - "CS9076" + - "CS9077" + - "CS9078" + - "CS9079" + - "CS9080" + - "CS9081" + - "CS9082" + - "CS9083" + - "CS9084" + - "CS9085" + - "CS9086" + - "CS9087" + - "CS9088" + - "CS9089" + - "CS9090" + - "CS9091" + - "CS9092" + - "CS9093" + - "CS9094" + - "CS9095" + - "CS9096" + - "CS9097" + - "CS9338" - "errors [C#], additional information" --- # Sorry, we don't have specifics on this C# error From 957176e06e7630fbd909afc6c867e37eebed90dc Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 13:32:17 -0500 Subject: [PATCH 03/12] Organize around themes --- .../generic-type-parameters-errors.md | 460 ++++++++++-------- 1 file changed, 261 insertions(+), 199 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index cb55d95d677d0..1d91a5177d711 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -55,24 +55,45 @@ This article covers the following compiler errors: -- [**CS0080**](#constraints-on-non-generic-declarations): *Constraints are not allowed on non-generic declarations* -- [**CS0081**](#type-parameter-declaration): *Type parameter declaration must be an identifier not a type* -- [**CS0224**](#vararg-method-restrictions): *A method with vararg cannot be generic, be in a generic type, or have a params parameter* -- [**CS0304**](#new-constraint): *Cannot create an instance of the variable type 'type' because it does not have the new() constraint* -- [**CS0305**](#type-argument-count): *Using the generic type 'generic type' requires 'number' type arguments* -- [**CS0306**](#invalid-type-argument): *The type 'type' may not be used as a type argument* -- [**CS0307**](#non-generic-construct): *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* -- [**CS0308**](#non-generic-construct): *The non-generic type-or-method 'identifier' cannot be used with type arguments.* -- [**CS0310**](#new-constraint): *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'* -- [**CS0311**](#constraint-conversions): *The type 'type1' cannot be used as type parameter 'T' in the generic type or method '\'. There is no implicit reference conversion from 'type1' to 'type2'.* -- [**CS0312**](#nullable-type-constraints): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.* -- [**CS0314**](#constraint-conversions): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* -- [**CS0313**](#nullable-type-constraints): *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints.* -- [**CS0413**](#type-parameter-with-as-operator): *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint* -- [**CS0417**](#new-constraint): *'identifier': cannot provide arguments when creating an instance of a variable type* -- [**CS0338**](#type-argument-visibility): *Inconsistent accessibility: type argument is less accessible than class.* - -## Constraints on non-generic declarations +- [**CS0080**](#type-parameter-declaration-and-naming): *Constraints are not allowed on non-generic declarations* +- [**CS0081**](#type-parameter-declaration-and-naming): *Type parameter declaration must be an identifier not a type* +- [**CS0224**](#type-argument-count-and-usage): *A method with vararg cannot be generic, be in a generic type, or have a params parameter* +- [**CS0304**](#constructor-constraints): *Cannot create an instance of the variable type 'type' because it does not have the new() constraint* +- [**CS0305**](#type-argument-count-and-usage): *Using the generic type 'generic type' requires 'number' type arguments* +- [**CS0306**](#type-argument-count-and-usage): *The type 'type' may not be used as a type argument* +- [**CS0307**](#type-argument-count-and-usage): *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* +- [**CS0308**](#type-argument-count-and-usage): *The non-generic type-or-method 'identifier' cannot be used with type arguments.* +- [**CS0310**](#constructor-constraints): *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'* +- [**CS0311**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'T' in the generic type or method '\'. There is no implicit reference conversion from 'type1' to 'type2'.* +- [**CS0312**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.* +- [**CS0313**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints.* +- [**CS0314**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* +- [**CS0315**](#constraint-satisfaction-and-conversions): *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\\'. There is no boxing conversion from 'valueType' to 'referenceType'.* +- [**CS0403**](#generic-type-usage-restrictions): *Cannot use null as type argument for 'type parameter' because it may not be a reference type. Consider using 'default' instead.* +- [**CS0412**](#type-parameter-declaration-and-naming): *'parameter': a parameter, local variable, or local function cannot have the same name as a method type parameter* +- [**CS0413**](#generic-type-usage-restrictions): *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint* +- [**CS0417**](#constructor-constraints): *'identifier': cannot provide arguments when creating an instance of a variable type* +- [**CS0694**](#type-parameter-declaration-and-naming): *Type parameter 'identifier' has the same name as the containing type, or method* +- [**CS0695**](#generic-type-usage-restrictions): *Generic class cannot implement both 'generic interface' and 'generic interface' because they may unify for some type parameter substitutions* +- [**CS0698**](#generic-type-usage-restrictions): *A generic type cannot derive from 'type' because it is an attribute class* +- [**CS9338**](#generic-type-usage-restrictions): *Type argument 'type' must be public because it is used in a public signature.* + +## Type parameter declaration and naming + +The following errors relate to how type parameters are declared and named in generic types and methods: + +- **CS0080**: *Constraints are not allowed on non-generic declarations* +- **CS0081**: *Type parameter declaration must be an identifier not a type* +- **CS0412**: *'generic': a parameter or local variable cannot have the same name as a method type parameter* +- **CS0694**: *Type parameter 'identifier' has the same name as the containing type, or method* + +Type parameters must be declared using identifiers (like `T` or `TInput`), not actual types. Constraints can only be applied to generic declarations. Additionally, type parameter names must not conflict with the containing type, method, or other parameters. + +For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). + +### Constraints on non-generic declarations + +The syntax found may only be used in a generic declaration to apply constraints to the type parameter. The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](../fundamentals/types/generics.md). @@ -100,9 +121,9 @@ namespace MyNamespace } ``` -## Type parameter declaration +### Type parameter declaration -When you declare a generic method or type, specify the type parameter as an identifier, for example "T" or "inputType". When client code calls the method, it supplies the type, which replaces each occurrence of the identifier in the method or class body. For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md). +When you declare a generic method or type, specify the type parameter as an identifier, for example "T" or "inputType". When client code calls the method, it supplies the type, which replaces each occurrence of the identifier in the method or class body. ```csharp // CS0081.cs @@ -120,47 +141,201 @@ class MyClass } ``` -## Vararg method restrictions +### Type parameter name conflicts + +Type parameter names must not conflict with other identifiers in scope. + +```csharp +// CS0412.cs +using System; + +class C +{ + // Parameter name is the same as method type parameter name + public void G(int T) // CS0412 + { + } + public void F() + { + // Method local variable name is the same as method type + // parameter name + double T = 0.0; // CS0412 + Console.WriteLine(T); + } + + public static void Main() + { + } +} +``` + +Type parameter names also cannot match the containing type or method name: + +```csharp +// CS0694.cs +// compile with: /target:library +class C {} // CS0694 + +class A +{ + public void F(F arg); // CS0694 +} +``` + +## Type argument count and usage + +The following errors relate to providing the correct number and type of type arguments to generic types and methods: + +- **CS0224**: *A method with vararg cannot be generic, be in a generic type, or have a params parameter* +- **CS0305**: *Using the generic type 'generic type' requires 'number' type arguments* +- **CS0306**: *The type 'type' may not be used as a type argument* +- **CS0307**: *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* +- **CS0308**: *The non-generic type-or-method 'identifier' cannot be used with type arguments.* + +These errors occur when generic types or methods are used incorrectly with type arguments, or when certain restrictions on generic declarations are violated. + +For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). + +### CS0224: Vararg method restrictions A method with vararg cannot be generic, be in a generic type, or have a params parameter. This restriction applies to methods that use the `__arglist` keyword for variable argument lists. -## new() constraint +### CS0305: Wrong number of type arguments -When you implement a generic class, and you want to use the `new` keyword to create a new instance of any type that is supplied for a type parameter `T`, you must apply the [new() constraint](../keywords/new-constraint.md) to `T` in the class declaration, as shown in the following example. +This error occurs when the expected number of type arguments was not found. Use the required number of type arguments for the generic type: ```csharp -class C where T : new() +// CS0305.cs +public class MyList {} +public class MyClass {} + +class MyClass +{ + public static void Main() + { + MyList list1 = new MyList(); // CS0305 + MyList list2 = new MyList(); // OK + } +} ``` -The `new()` constraint enforces type safety by guaranteeing that any concrete type that is supplied for `T` has a parameterless constructor. CS0304 occurs if you attempt to use the `new` operator in the body of the class to create an instance of type parameter `T` when `T` does not specify the `new()` constraint. On the client side, if code attempts to instantiate the generic class with a type that has no parameterless constructor, that code will generate CS0310. +### CS0306: Invalid type argument -The following example generates CS0304. +Certain types cannot be used as type arguments. This error commonly occurs when pointer types are used as type arguments: ```csharp -// CS0304.cs -// Compile with: /target:library. +// CS0306.cs class C { - // The following line generates CS0304. - T t = new T(); } + +class M +{ + // CS0306 – int* not allowed as a type parameter + C f; +} +``` + +### CS0307 and CS0308: Non-generic construct with type arguments + +The construct named was not a generic type or method, but it was used with type arguments. Remove the type arguments in angle brackets, or redeclare the construct as a generic type or method. + +The following sample generates CS0307: + +```csharp +// CS0307.cs +class C +{ + public int P { get { return 1; } } + public static void Main() + { + C c = new C(); + int p = c.P(); // CS0307 – C.P is a property + // Try this instead + // int p = c.P; + } +} +``` + +The following example generates CS0308: + +```csharp +// CS0308a.cs +class MyClass +{ + public void F() {} + public static void Main() + { + F(); // CS0308 – F is not generic. + // Try this instead: + // F(); + } +} +``` + +The following example also generates CS0308. To resolve the error, use the directive "using System.Collections.Generic": + +```csharp +// CS0308b.cs +// compile with: /t:library +using System.Collections; +// To resolve, uncomment the following line: +// using System.Collections.Generic; +public class MyStack +{ + // Store the elements of the stack: + private T[] items = new T[100]; + private int stack_counter = 0; + + // Define the iterator block: + public IEnumerator GetEnumerator() // CS0308 + { + for (int i = stack_counter - 1 ; i >= 0; i--) + yield return items[i]; + } +} +``` + +## Constructor constraints + +The following errors relate to the `new()` constraint on generic type parameters: + +- **CS0304**: *Cannot create an instance of the variable type 'type' because it does not have the new() constraint* +- **CS0310**: *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'* +- **CS0417**: *'identifier': cannot provide arguments when creating an instance of a variable type* + +When you implement a generic class and want to use the `new` keyword to create a new instance of any type that is supplied for a type parameter `T`, you must apply the [new() constraint](../keywords/new-constraint.md) to `T` in the class declaration, as shown in the following example. + +```csharp +class C where T : new() ``` -The `new` operator also is not allowed in methods of the class. +The `new()` constraint enforces type safety by guaranteeing that any concrete type that is supplied for `T` has a parameterless constructor. + +### CS0304: Missing new() constraint + +This error occurs if you attempt to use the `new` operator to create an instance of type parameter `T` when `T` does not specify the `new()` constraint: ```csharp +// CS0304.cs // Compile with: /target:library. class C +{ + // The following line generates CS0304. + T t = new T(); +} + +class C2 { public void ExampleMethod() { - // The following line generates CS0304. + // The following line also generates CS0304. T t = new T(); } } ``` -To avoid the error, declare the class by using the `new()` constraint, as shown in the following example. +To avoid the error, declare the class by using the `new()` constraint: ```csharp // Compile with: /target:library. @@ -175,9 +350,9 @@ class C where T : new() } ``` -The generic type or method defines the `new()` constraint in its `where` clause, so any type must have a public parameterless constructor in order to be used as a type argument for that generic type or method. To avoid this error, make sure that the type has the correct constructor, or modify the constraint clause of the generic type or method. +### CS0310: Type must have parameterless constructor -The following sample generates CS0310: +When a generic type or method defines the `new()` constraint in its `where` clause, any type argument must have a public parameterless constructor. The following sample generates CS0310: ```csharp // CS0310.cs @@ -211,9 +386,11 @@ class CMain } ``` -This error occurs if a call to the `new` operator on a type parameter has arguments. The only constructor that can be called by using the `new` operator on an unknown parameter type is a constructor that has no arguments. If you need to call another constructor, consider using a class type constraint or interface constraint. +To avoid this error, make sure the type has the correct constructor, or modify the constraint clause of the generic type or method. + +### CS0417: Cannot provide arguments to constructor -The following example generates CS0417: +This error occurs if a call to the `new` operator on a type parameter has arguments. The only constructor that can be called using the `new` operator on an unknown parameter type is a parameterless constructor. If you need to call another constructor, consider using a class type constraint or interface constraint: ```csharp // CS0417 @@ -227,49 +404,9 @@ class ExampleClass where T : new() } ``` -## Type argument count - -This error occurs when the expected number of type arguments was not found. To resolve C0305, use the required number of type arguments. - -## Example - -The following sample generates CS0305. - -```csharp -// CS0305.cs -public class MyList {} -public class MyClass {} - -class MyClass -{ - public static void Main() - { - MyList list1 = new MyList(); // CS0305 - MyList list2 = new MyList(); // OK - } -} -``` - -## Invalid type argument - -The type used as a type parameter is not allowed. This could be because the type is a pointer type. - -The following example generates CS0306: +For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). -```csharp -// CS0306.cs -class C -{ -} - -class M -{ - // CS0306 – int* not allowed as a type parameter - C f; -} -``` - -## Non-generic construct +## Constraint satisfaction and conversions The construct named was not a type or a method, the only constructs that can take generic arguments. Remove the type arguments in angle brackets. If a generic is needed, declare your generic construct as a generic type or method. @@ -331,17 +468,23 @@ public class MyStack } ``` -## Constraint conversions +## Constraint satisfaction and conversions -When a constraint is applied to a generic type parameter, an implicit identity or reference conversion must exist from the concrete argument to the type of the constraint. +The following errors relate to type arguments not satisfying the constraints of generic type parameters: -## To correct this error +- **CS0311**: *The type 'type1' cannot be used as type parameter 'T' in the generic type or method '\'. There is no implicit reference conversion from 'type1' to 'type2'.* +- **CS0312**: *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.* +- **CS0313**: *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints.* +- **CS0314**: *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* +- **CS0315**: *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\\'. There is no boxing conversion from 'valueType' to 'referenceType'.* + +When a constraint is applied to a generic type parameter, an implicit identity or reference conversion must exist from the concrete argument to the type of the constraint. -1. Change the type argument you are using to one that fulfills the constraint. +For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). -2. If you own the class, you can remove the constraint or else do something to enable an implicit reference or identity conversion. For example, you can make the second type inherit from the first. +### CS0311: No implicit reference conversion -## Example +To correct this error, change the type argument to one that fulfills the constraint, or enable an implicit reference or identity conversion (for example, make the second type inherit from the first): ```csharp // cs0311.cs @@ -359,17 +502,11 @@ class Program } ``` -If this error occurs when trying to use a value-type argument, notice that an implicit numeric conversion, for example from `short` to `int`, does not satisfy a generic type parameter. +Note that an implicit numeric conversion (such as from `short` to `int`) does not satisfy a generic type parameter constraint. -When a generic type uses a type parameter that is constrained, the new class must also satisfy those same constraints. +### CS0314: Type parameter constraint not satisfied in derived class -## To correct this error - -1. In the example that follows, add `where T : ClassConstraint` to class `B`. - -## Example - -The following code generates CS0314: +When a generic type uses a type parameter that is constrained, any derived class must also satisfy those same constraints. Add the constraint to the derived class: ```csharp // cs0314.cs @@ -382,20 +519,14 @@ public class A where T : ClassConstraint public class B : A //CS0314 { } -// Try using this instead. +// Correct: Add the constraint to the derived class public class C : A where T : ClassConstraint { } ``` -This error occurs when you constrain a generic type to a particular class, and try to construct an instance of that class by using a value type that cannot be implicitly boxed to it. - -## To correct this error +### CS0315: No boxing conversion -1. One solution is to redefine the struct as a class. - -## Example - -The following example generates CS0315: +This error occurs when you try to use a value type as a type argument where a reference type is required by the constraint. One solution is to redefine the struct as a class: ```csharp // cs0315.cs @@ -415,17 +546,9 @@ public class Test } ``` -## Nullable type constraints - -A nullable value type is distinct from its non-nullable counterpart; no implicit reference conversion or identify conversion exists between them. A nullable boxing conversion does not satisfy a generic type constraint. In the example that follows, the first type parameter is a `Nullable` and the second type parameter is a `System.Int32`. +### CS0312 and CS0313: Nullable type constraints -## To correct this error - -1. Remove the constraint. - -2. In the following example, make the second type argument either `int?` or `object`. - -## Example +A nullable value type is distinct from its non-nullable counterpart. No implicit reference conversion or identity conversion exists between them, and a nullable boxing conversion does not satisfy a generic type constraint. The following code generates CS0312: @@ -443,21 +566,9 @@ class Program } ``` -Although a nullable value type is distinct from a non-nullable type, various kinds of conversions are allowed between nullable and non-nullable values. - -A nullable value type is not equivalent to its non-nullable counterpart. In the example that follows, `ImplStruct` satisfies the `BaseInterface` constraint but `ImplStruct?` does not because `Nullable` does not implement `BaseInterface`. - -## To correct this error - -1. Using the code that follows as an example, one solution is to specify an ordinary `ImplStruct` as the first type argument in the call to `TestMethod`. Then modify `TestMethod` to create a nullable version of `Implstruct` in its return statement: - - ```csharp - return new Nullable(t); - ``` - -## Example +To correct this error, remove the constraint or make the second type argument either `int?` or `object`. -The following code generates CS0313: +Similarly, a nullable value type cannot satisfy interface constraints. The following code generates CS0313: ```csharp // cs0313.cs @@ -485,11 +596,23 @@ public class NullableTest } ``` -## Null assignment to type parameter +One solution is to specify an ordinary `ImplStruct` as the first type argument, then modify `TestMethod` to create a nullable version in its return statement using `return new Nullable(t);`. -You cannot assign null to the unknown type named because it might be a value type, which does not allow null assignment. If your generic class is not intended to accept value types, use the class type constraint. If it can accept value types, such as the built-in types, you may be able to replace the assignment to null with the expression `default(T)`, as shown in the following example. +## Generic type usage restrictions -## Example +The following errors relate to restrictions on how generic types can be used: + +- **CS0403**: *Cannot use null as type argument for 'type parameter' because it may not be a reference type. Consider using 'default' instead.* +- **CS0413**: *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint* +- **CS0695**: *Generic class cannot implement both 'generic interface' and 'generic interface' because they may unify for some type parameter substitutions* +- **CS0698**: *A generic type cannot derive from 'type' because it is an attribute class* +- **CS9338**: *Type argument 'type' must be public because it is used in a public signature.* + +For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). + +### CS0403: Null assignment to type parameter + +You cannot assign null to an unconstrained type parameter because it might be a value type, which does not allow null assignment. If your generic class is not intended to accept value types, use the class type constraint. If it can accept value types, such as the built-in types, you may be able to replace the assignment to null with the expression `default(T)`. The following sample generates CS0403. @@ -514,46 +637,12 @@ class D where T : class } ``` -## Type parameter name conflict - -There is a name conflict between the type parameter of a generic method and a local variable in the method or one of the method's parameters. To avoid this error, rename any conflicting parameters or local variables. - -## Example - -The following sample generates CS0412: - -```csharp -// CS0412.cs -using System; - -class C -{ - // Parameter name is the same as method type parameter name - public void G(int T) // CS0412 - { - } - public void F() - { - // Method local variable name is the same as method type - // parameter name - double T = 0.0; // CS0412 - Console.WriteLine(T); - } - - public static void Main() - { - } -} -``` - -## Type parameter with as operator +### CS0413: Type parameter with as operator This error occurs if a generic type uses the [as](../operators/type-testing-and-cast.md#the-as-operator) operator, but that generic type does not have a class type constraint. The `as` operator is only allowed with reference and nullable value types, so the type parameter must be constrained to guarantee that it is not a value type. To avoid this error, use a class type constraint or a reference type constraint. This is because the `as` operator could return `null`, which is not a possible value for a value type, and the type parameter must be treated as a value type unless it is a class type constraint or a reference type constraint. -## Example - The following sample generates CS0413. ```csharp @@ -580,38 +669,11 @@ class CMain } ``` -## Type parameter same name as containing type or method - -You must use a different name for the type parameter since the type parameter's name cannot be identical to the type or method name that contains the type parameter. - -### Example 1 - -The following sample generates CS0694. - -```csharp -// CS0694.cs -// compile with: /target:library -class C {} // CS0694 -``` - -### Example 2 - -In addition to the above case involving a generic class, this error may occur with a method: - -```csharp -// CS0694_2.cs -// compile with: /target:library -class A -{ - public void F(F arg); // CS0694 -} -``` - -## Cannot implement same generic interface with different type parameters +### CS0695: Cannot implement same generic interface with different type parameters This error occurs when a generic class implements more than one parameterization of the same generic interface, and there exists a type parameter substitution which would make the two interfaces identical. To avoid this error, implement only one of the interfaces, or change the type parameters to avoid the conflict. -The following sample generates CS0695: +The following sample generates CS0695. ```csharp // CS0695.cs @@ -626,11 +688,11 @@ class G : I, I // CS0695 } ``` -## Generic type cannot derive from attribute +### CS0698: Generic type cannot derive from attribute Any class that derives from an attribute class is an attribute. Attributes are not allowed to be generic types. -The following sample generates CS0698: +The following sample generates CS0698. ```csharp // CS0698.cs @@ -639,8 +701,8 @@ class C : System.Attribute // CS0698 } ``` -## Type argument visibility +### CS9338: Type argument visibility -The following errors relate to accessibilty of type arguments: +Type arguments used in public signatures must themselves be publicly accessible. This error occurs when you use a non-public type as a type argument in a public context. -- **CS0338**: *Inconsistent accessibility: type argument is less accessible than class.* +Ensure that all type arguments used in public or protected members are themselves public: From e090ed66732ef18deb6d71d859a785afbe2076f1 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 13:47:42 -0500 Subject: [PATCH 04/12] First edit pass on themes. --- .github/prompts/error-consolidation.md | 2 +- .../generic-type-parameters-errors.md | 596 +----------------- 2 files changed, 30 insertions(+), 568 deletions(-) diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 510d21a094614..aa1d4db2f7c56 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -64,7 +64,7 @@ Understand these instructions, then suggest a list of themes and the included er ## Move from description to resolution -Rework the highlighted section so the focus is on how to correct each error. This article doesn't need to explain the associated language feature. Instead, in each section, provide links to language reference or language specification material that explains the rules violated when these diagnostics appear. Add explanatory context after each correction (in parentheses with the error code). Provided brief reasons why each correction is needed. Use detailed, sentence-style explanations rather than brief imperative statements. For each recommendation put the affectived error codes in parentheses, and in **bold** style. Remove extensive examples. +Rework the highlighted section so the focus is on how to correct each error. This article doesn't need to explain the associated language feature. Instead, in each section, provide links to language reference or language specification material that explains the rules violated when these diagnostics appear. Add explanatory context after each correction (in parentheses with the error code). Provided brief reasons why each correction is needed. Use detailed, sentence-style explanations rather than brief imperative statements. For each recommendation put the affectived error codes in parentheses, and in **bold** style. Remove extensive examples. Remove all H3 headings in this section. If any errors are no longer produced in the latest version of C#, make a note of that. ## Verify error messages diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index 1d91a5177d711..719b1a4ca130e 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -87,100 +87,13 @@ The following errors relate to how type parameters are declared and named in gen - **CS0412**: *'generic': a parameter or local variable cannot have the same name as a method type parameter* - **CS0694**: *Type parameter 'identifier' has the same name as the containing type, or method* -Type parameters must be declared using identifiers (like `T` or `TInput`), not actual types. Constraints can only be applied to generic declarations. Additionally, type parameter names must not conflict with the containing type, method, or other parameters. +To correct these errors, ensure that type parameters are declared with valid identifiers, constraint clauses are only applied to generic declarations, and type parameter names don't conflict with other identifiers in scope: -For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). +- Remove the constraint clause from non-generic declarations (**CS0080**). The `where` clause can only be used on generic types and methods that declare type parameters, because constraints define requirements that type arguments must satisfy. If you need to apply constraints, first add type parameters to your type or method declaration. For example, change `public class MyClass where MyClass : System.IDisposable` to `public class MyClass where T : System.IDisposable`. +- Replace actual type names with identifiers in type parameter declarations (**CS0081**). Type parameters must be declared using identifiers (like `T`, `TKey`, or `TValue`) rather than concrete types (like `int` or `string`), because the purpose of a type parameter is to serve as a placeholder that will be substituted with actual types when the generic type or method is used. For example, change `public void F()` to `public void F()`. +- Rename type parameters, local variables, or parameters to avoid naming conflicts (**CS0412**, **CS0694**). Type parameter names must not shadow other identifiers in the same scope, and they must not match the name of the containing type or method, because such conflicts create ambiguity about which identifier is being referenced. For example, if you have a method `public void F()`, you cannot declare a local variable `double T` inside that method, and you cannot name a type parameter the same as its containing type (`class C`). -### Constraints on non-generic declarations - -The syntax found may only be used in a generic declaration to apply constraints to the type parameter. - -The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](../fundamentals/types/generics.md). - -The following sample generates CS0080 because MyClass is not a generic class and Foo is not a generic method. - -```csharp -namespace MyNamespace -{ - public class MyClass where MyClass : System.IDisposable // CS0080 //the following line shows an example of correct syntax - //public class MyClass where T : System.IDisposable - { - public void Foo() where Foo : new() // CS0080 - //the following line shows an example of correct syntax - //public void Foo() where U : struct - { - } - } - - public class Program - { - public static void Main() - { - } - } -} -``` - -### Type parameter declaration - -When you declare a generic method or type, specify the type parameter as an identifier, for example "T" or "inputType". When client code calls the method, it supplies the type, which replaces each occurrence of the identifier in the method or class body. - -```csharp -// CS0081.cs -class MyClass -{ - public void F() {} // CS0081 - public void F(T input) {} // OK - - public static void Main() - { - MyClass a = new MyClass(); - a.F(2); - a.F(.05); - } -} -``` - -### Type parameter name conflicts - -Type parameter names must not conflict with other identifiers in scope. - -```csharp -// CS0412.cs -using System; - -class C -{ - // Parameter name is the same as method type parameter name - public void G(int T) // CS0412 - { - } - public void F() - { - // Method local variable name is the same as method type - // parameter name - double T = 0.0; // CS0412 - Console.WriteLine(T); - } - - public static void Main() - { - } -} -``` - -Type parameter names also cannot match the containing type or method name: - -```csharp -// CS0694.cs -// compile with: /target:library -class C {} // CS0694 - -class A -{ - public void F(F arg); // CS0694 -} -``` +For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). ## Type argument count and usage @@ -192,109 +105,14 @@ The following errors relate to providing the correct number and type of type arg - **CS0307**: *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* - **CS0308**: *The non-generic type-or-method 'identifier' cannot be used with type arguments.* -These errors occur when generic types or methods are used incorrectly with type arguments, or when certain restrictions on generic declarations are violated. +To correct these errors, ensure that you provide the exact number of type arguments required by the generic declaration, use only valid types as type arguments, and don't apply type arguments to non-generic constructs: -For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). +- Remove generic type parameters or containing generic type declarations from methods that use `__arglist` (**CS0224**). The `__arglist` keyword is incompatible with generics because the runtime mechanisms for handling variable argument lists conflict with the type substitution required for generic type parameters. This restriction also applies to the `params` keyword when used in combination with generic methods or methods within generic types. +- Supply the exact number of type arguments specified in the generic type or method declaration (**CS0305**). Each generic type parameter declared in the definition must have a corresponding type argument when the generic type is instantiated, because the compiler needs to know which concrete type to substitute for each type parameter. For example, if a class is declared as `class MyList`, you must provide exactly one type argument when using it, such as `MyList`, not `MyList`. +- Use only valid types as type arguments (**CS0306**). Pointer types, such as `int*` or `char*`, cannot be used as type arguments because generic types require managed types that the garbage collector can track, and pointer types are unmanaged. If you need to work with pointers in a generic context, consider using `IntPtr` or restructuring your code to avoid mixing generics with unsafe code. +- Remove type argument syntax from non-generic constructs (**CS0307**, **CS0308**). Type arguments enclosed in angle brackets (like ``) can only be applied to generic types and methods that declare type parameters. If you attempt to use this syntax on properties, non-generic methods, or other non-generic constructs, you must either remove the type arguments entirely or ensure you have imported the correct namespace that contains the generic version of the type. For example, `IEnumerator` requires the `using System.Collections.Generic;` directive, whereas `IEnumerator` is in `System.Collections`. -### CS0224: Vararg method restrictions - -A method with vararg cannot be generic, be in a generic type, or have a params parameter. This restriction applies to methods that use the `__arglist` keyword for variable argument lists. - -### CS0305: Wrong number of type arguments - -This error occurs when the expected number of type arguments was not found. Use the required number of type arguments for the generic type: - -```csharp -// CS0305.cs -public class MyList {} -public class MyClass {} - -class MyClass -{ - public static void Main() - { - MyList list1 = new MyList(); // CS0305 - MyList list2 = new MyList(); // OK - } -} -``` - -### CS0306: Invalid type argument - -Certain types cannot be used as type arguments. This error commonly occurs when pointer types are used as type arguments: - -```csharp -// CS0306.cs -class C -{ -} - -class M -{ - // CS0306 – int* not allowed as a type parameter - C f; -} -``` - -### CS0307 and CS0308: Non-generic construct with type arguments - -The construct named was not a generic type or method, but it was used with type arguments. Remove the type arguments in angle brackets, or redeclare the construct as a generic type or method. - -The following sample generates CS0307: - -```csharp -// CS0307.cs -class C -{ - public int P { get { return 1; } } - public static void Main() - { - C c = new C(); - int p = c.P(); // CS0307 – C.P is a property - // Try this instead - // int p = c.P; - } -} -``` - -The following example generates CS0308: - -```csharp -// CS0308a.cs -class MyClass -{ - public void F() {} - public static void Main() - { - F(); // CS0308 – F is not generic. - // Try this instead: - // F(); - } -} -``` - -The following example also generates CS0308. To resolve the error, use the directive "using System.Collections.Generic": - -```csharp -// CS0308b.cs -// compile with: /t:library -using System.Collections; -// To resolve, uncomment the following line: -// using System.Collections.Generic; -public class MyStack -{ - // Store the elements of the stack: - private T[] items = new T[100]; - private int stack_counter = 0; - - // Define the iterator block: - public IEnumerator GetEnumerator() // CS0308 - { - for (int i = stack_counter - 1 ; i >= 0; i--) - yield return items[i]; - } -} -``` +For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). ## Constructor constraints @@ -304,169 +122,13 @@ The following errors relate to the `new()` constraint on generic type parameters - **CS0310**: *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'* - **CS0417**: *'identifier': cannot provide arguments when creating an instance of a variable type* -When you implement a generic class and want to use the `new` keyword to create a new instance of any type that is supplied for a type parameter `T`, you must apply the [new() constraint](../keywords/new-constraint.md) to `T` in the class declaration, as shown in the following example. - -```csharp -class C where T : new() -``` - -The `new()` constraint enforces type safety by guaranteeing that any concrete type that is supplied for `T` has a parameterless constructor. - -### CS0304: Missing new() constraint - -This error occurs if you attempt to use the `new` operator to create an instance of type parameter `T` when `T` does not specify the `new()` constraint: - -```csharp -// CS0304.cs -// Compile with: /target:library. -class C -{ - // The following line generates CS0304. - T t = new T(); -} - -class C2 -{ - public void ExampleMethod() - { - // The following line also generates CS0304. - T t = new T(); - } -} -``` - -To avoid the error, declare the class by using the `new()` constraint: - -```csharp -// Compile with: /target:library. -class C where T : new() -{ - T t = new T(); - - public void ExampleMethod() - { - T t = new T(); - } -} -``` - -### CS0310: Type must have parameterless constructor - -When a generic type or method defines the `new()` constraint in its `where` clause, any type argument must have a public parameterless constructor. The following sample generates CS0310: - -```csharp -// CS0310.cs -using System; - -class G where T : new() -{ - T t; - - public G() - { - t = new T(); - Console.WriteLine(t); - } -} - -class B -{ - private B() { } - // Try this instead: - // public B() { } -} - -class CMain -{ - public static void Main() - { - G g = new G(); // CS0310 - Console.WriteLine(g.ToString()); - } -} -``` - -To avoid this error, make sure the type has the correct constructor, or modify the constraint clause of the generic type or method. - -### CS0417: Cannot provide arguments to constructor - -This error occurs if a call to the `new` operator on a type parameter has arguments. The only constructor that can be called using the `new` operator on an unknown parameter type is a parameterless constructor. If you need to call another constructor, consider using a class type constraint or interface constraint: - -```csharp -// CS0417 -class ExampleClass where T : new() -{ - // The following line causes CS0417. - T instance1 = new T(1); - - // The following line doesn't cause the error. - T instance2 = new T(); -} -``` - -For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). +To correct these errors, add the `new()` constraint to type parameters that need to be instantiated, ensure type arguments have public parameterless constructors, and avoid passing arguments when constructing instances of type parameters: -## Constraint satisfaction and conversions +- Add the `new()` constraint to the type parameter declaration (**CS0304**). When you use the `new` operator to create an instance of a type parameter within a generic type or method, the compiler must be able to guarantee that any type argument supplied at runtime will have a parameterless constructor available. The `new()` constraint provides this guarantee at compile time, allowing the compiler to generate the appropriate instantiation code. For example, if you have `class C` with a member `T t = new T();`, you must change the declaration to `class C where T : new()`. +- Ensure that type arguments used with `new()` constrained type parameters have public parameterless constructors (**CS0310**). When a generic type or method declares a `new()` constraint on a type parameter, any concrete type used as a type argument must be non-abstract and must provide a public parameterless constructor. If a type only has non-public constructors (such as `private` or `protected`) or only has constructors with parameters, it cannot satisfy the `new()` constraint. To fix this error, either add a public parameterless constructor to the type, or use a different type argument that already has one. +- Remove constructor arguments when instantiating type parameters (**CS0417**). The `new()` constraint only guarantees the existence of a parameterless constructor, so attempting to pass arguments to `new T(arguments)` is not allowed because the compiler cannot verify that a constructor with those specific parameter types exists on the type that will eventually be substituted for `T`. If you need to construct instances with specific arguments, consider using factory methods, abstract factory patterns, or specific base class/interface constraints that define the construction behavior you need. -The construct named was not a type or a method, the only constructs that can take generic arguments. Remove the type arguments in angle brackets. If a generic is needed, declare your generic construct as a generic type or method. - -The method or type is not generic, but it was used with type arguments. To avoid this error, remove the angled brackets and type arguments, or redeclare the method or type as a generic method or type. - -The following sample generates CS0307: - -```csharp -// CS0307.cs -class C -{ - public int P { get { return 1; } } - public static void Main() - { - C c = new C(); - int p = c.P(); // CS0307 – C.P is a property - // Try this instead - // int p = c.P; - } -} -``` - -The following example generates CS0308: - -```csharp -// CS0308a.cs -class MyClass -{ - public void F() {} - public static void Main() - { - F(); // CS0308 – F is not generic. - // Try this instead: - // F(); - } -} -``` - -The following example also generates CS0308. To resolve the error, use the directive "using System.Collections.Generic." - -```csharp -// CS0308b.cs -// compile with: /t:library -using System.Collections; -// To resolve, uncomment the following line: -// using System.Collections.Generic; -public class MyStack -{ - // Store the elements of the stack: - private T[] items = new T[100]; - private int stack_counter = 0; - - // Define the iterator block: - public IEnumerator GetEnumerator() // CS0308 - { - for (int i = stack_counter - 1 ; i >= 0; i--) - yield return items[i]; - } -} -``` +For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md) and the [new() constraint](../keywords/new-constraint.md). ## Constraint satisfaction and conversions @@ -478,125 +140,14 @@ The following errors relate to type arguments not satisfying the constraints of - **CS0314**: *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* - **CS0315**: *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\\'. There is no boxing conversion from 'valueType' to 'referenceType'.* -When a constraint is applied to a generic type parameter, an implicit identity or reference conversion must exist from the concrete argument to the type of the constraint. - -For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). - -### CS0311: No implicit reference conversion - -To correct this error, change the type argument to one that fulfills the constraint, or enable an implicit reference or identity conversion (for example, make the second type inherit from the first): - -```csharp -// cs0311.cs -class B {} -class C {} -class Test where T : C -{ } - -class Program -{ - static void Main() - { - Test test = new Test(); //CS0311 - } -} -``` - -Note that an implicit numeric conversion (such as from `short` to `int`) does not satisfy a generic type parameter constraint. - -### CS0314: Type parameter constraint not satisfied in derived class - -When a generic type uses a type parameter that is constrained, any derived class must also satisfy those same constraints. Add the constraint to the derived class: - -```csharp -// cs0314.cs -// Compile with: /target:library -public class ClassConstraint { } - -public class A where T : ClassConstraint -{ } - -public class B : A //CS0314 -{ } - -// Correct: Add the constraint to the derived class -public class C : A where T : ClassConstraint -{ } -``` - -### CS0315: No boxing conversion - -This error occurs when you try to use a value type as a type argument where a reference type is required by the constraint. One solution is to redefine the struct as a class: - -```csharp -// cs0315.cs -public class ClassConstraint { } -public struct ViolateClassConstraint { } - -public class Gen where T : ClassConstraint -{ -} -public class Test -{ - public static int Main() - { - Gen g = new Gen(); //CS0315 - return 1; - } -} -``` - -### CS0312 and CS0313: Nullable type constraints - -A nullable value type is distinct from its non-nullable counterpart. No implicit reference conversion or identity conversion exists between them, and a nullable boxing conversion does not satisfy a generic type constraint. - -The following code generates CS0312: - -```csharp -// cs0312.cs -class Program -{ - static void MTyVar() where T : U { } - - static int Main() - { - MTyVar(); // CS0312 - return 1; - } -} -``` +To correct these errors, use type arguments that satisfy all constraints through appropriate conversions, ensure derived classes repeat base class constraints, and understand that nullable value types have special constraint requirements: -To correct this error, remove the constraint or make the second type argument either `int?` or `object`. +- Change the type argument to one that has an implicit reference conversion to the constraint type (**CS0311**). When a type parameter has a constraint like `where T : BaseType`, any type argument must be convertible to `BaseType` through an implicit reference conversion or identity conversion. This means the type argument must either be `BaseType` itself, derive from `BaseType`, or implement `BaseType` if it's an interface. Note that implicit numeric conversions (such as from `short` to `int`) do not satisfy generic type parameter constraints because these are value conversions, not reference conversions. +- Repeat the base class's type parameter constraints in any derived class declaration (**CS0314**). When a derived generic class inherits from a base generic class that has constraints on its type parameters, the derived class must declare the same constraints on its corresponding type parameters. This repetition is required because the compiler needs to verify that type arguments supplied to the derived class will also satisfy the requirements of the base class. For example, if you have `public class A where T : SomeClass`, then any class deriving from it must be declared as `public class B : A where T : SomeClass`. +- Use non-nullable value types or change the constraint type (**CS0312**, **CS0313**). Nullable value types (such as `int?`) are distinct from their underlying value types and do not satisfy the same constraints. There is no implicit conversion between `int?` and `int`, and nullable value types cannot satisfy interface constraints because the nullable wrapper itself doesn't implement the interface, even though the underlying value type does. To fix these errors, either use the non-nullable form of the value type as the type argument, or adjust your constraint to accept `object` or a nullable reference type if appropriate. +- Ensure type arguments satisfy reference type or class constraints (**CS0315**). When a type parameter is constrained to a class type (such as `where T : SomeClass`), you cannot use a value type (struct) as the type argument because there is no boxing conversion that satisfies the constraint relationship. The constraint requires a reference type that has an inheritance or implementation relationship with the constraint type. To resolve this error, either change the struct to a class if semantically appropriate, or remove the class constraint if the generic type can work with value types. -Similarly, a nullable value type cannot satisfy interface constraints. The following code generates CS0313: - -```csharp -// cs0313.cs -public interface BaseInterface { } -public struct ImplStruct : BaseInterface { } - -public class TestClass -{ - public T? TestMethod(T t) where T : struct, U - { - return t; - } -} - -public class NullableTest -{ - public static void Run() - { - - TestClass tc = new TestClass(); - tc.TestMethod(new ImplStruct?()); // CS0313 - } - public static void Main() - { } -} -``` - -One solution is to specify an ordinary `ImplStruct` as the first type argument, then modify `TestMethod` to create a nullable version in its return statement using `return new Nullable(t);`. +For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md) and [Implicit conversions](../../standard/base-types/conversion-tables.md). ## Generic type usage restrictions @@ -608,101 +159,12 @@ The following errors relate to restrictions on how generic types can be used: - **CS0698**: *A generic type cannot derive from 'type' because it is an attribute class* - **CS9338**: *Type argument 'type' must be public because it is used in a public signature.* -For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md). - -### CS0403: Null assignment to type parameter - -You cannot assign null to an unconstrained type parameter because it might be a value type, which does not allow null assignment. If your generic class is not intended to accept value types, use the class type constraint. If it can accept value types, such as the built-in types, you may be able to replace the assignment to null with the expression `default(T)`. - -The following sample generates CS0403. - -```csharp -// CS0403.cs -// compile with: /target:library -class C -{ - public void f() - { - T t = null; // CS0403 - T t2 = default(T); // OK - } -} - -class D where T : class -{ - public void f() - { - T t = null; // OK - } -} -``` - -### CS0413: Type parameter with as operator - -This error occurs if a generic type uses the [as](../operators/type-testing-and-cast.md#the-as-operator) operator, but that generic type does not have a class type constraint. The `as` operator is only allowed with reference and nullable value types, so the type parameter must be constrained to guarantee that it is not a value type. To avoid this error, use a class type constraint or a reference type constraint. - -This is because the `as` operator could return `null`, which is not a possible value for a value type, and the type parameter must be treated as a value type unless it is a class type constraint or a reference type constraint. - -The following sample generates CS0413. - -```csharp -// CS0413.cs -// compile with: /target:library -class A {} -class B : A {} - -class CMain -{ - A a = null; - public void G() - { - a = new A(); - System.Console.WriteLine (a as T); // CS0413 - } - - // OK - public void H() where T : A - { - a = new A(); - System.Console.WriteLine (a as T); - } -} -``` - -### CS0695: Cannot implement same generic interface with different type parameters - -This error occurs when a generic class implements more than one parameterization of the same generic interface, and there exists a type parameter substitution which would make the two interfaces identical. To avoid this error, implement only one of the interfaces, or change the type parameters to avoid the conflict. - -The following sample generates CS0695. - -```csharp -// CS0695.cs -// compile with: /target:library - -interface I -{ -} - -class G : I, I // CS0695 -{ -} -``` - -### CS0698: Generic type cannot derive from attribute - -Any class that derives from an attribute class is an attribute. Attributes are not allowed to be generic types. - -The following sample generates CS0698. - -```csharp -// CS0698.cs -class C : System.Attribute // CS0698 -{ -} -``` - -### CS9338: Type argument visibility +To correct these errors, use `default` instead of `null` for unconstrained type parameters, add class constraints when using the `as` operator, avoid interface unification conflicts, don't create generic attribute classes, and ensure type arguments match the visibility of their containing members: -Type arguments used in public signatures must themselves be publicly accessible. This error occurs when you use a non-public type as a type argument in a public context. +- Replace `null` assignments with `default(T)` or add a class constraint (**CS0403**). When you assign `null` to an unconstrained type parameter, the compiler cannot guarantee that the type argument will be a reference type that accepts `null` values, because it might be a value type like `int` or `struct`, which cannot be `null`. To resolve this error, either use `default(T)` which provides the appropriate default value for any type (null for reference types, zero or empty for value types), or add a `class` constraint to the type parameter if you specifically need reference type semantics and want to allow `null` assignments. +- Add a `class` or specific type constraint when using the `as` operator (**CS0413**). The `as` operator performs a safe type cast that returns `null` if the conversion fails, but this behavior is incompatible with value types because value types cannot be `null`. When you use `as` with an unconstrained type parameter, the compiler cannot guarantee the type argument won't be a value type, so it rejects the code. To fix this error, add a `class` constraint or a specific reference type constraint (like `where T : SomeClass`) to ensure the type parameter will always be a reference type that can properly handle the `null` result of a failed cast. +- Avoid implementing the same generic interface multiple times with type parameters that could unify (**CS0695**). When a class implements a generic interface multiple times with different type parameters (such as `class G : I, I`), there's a risk that someone could instantiate it with the same type for both parameters (`G`), which would create a conflict because the class would effectively be implementing `I` twice. To resolve this error, either implement the interface only once, restructure your type parameters to prevent unification, or use separate non-generic classes for different specializations. +- Remove generic type parameters from attribute classes (**CS0698**). Attributes are metadata annotations that are stored in compiled assemblies and must be instantiated by the runtime's reflection system, which doesn't support generic attributes because of the complexity this would introduce in attribute resolution and application. If you need attribute-like behavior with type parameters, consider using factory patterns, creating separate non-generic attribute classes for each needed type, or redesigning your solution to avoid generic attributes entirely. +- Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). When you declare a public or protected member that uses a generic type with specific type arguments, those type arguments must themselves be publicly accessible, because otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but cannot properly work with it because they cannot see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. -Ensure that all type arguments used in public or protected members are themselves public: +For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../programming-guide/concepts/attributes/index.md). From bd4721fb6bb608bb25ffc188ce722b77be9c17bb Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 14:03:38 -0500 Subject: [PATCH 05/12] Second edit pass --- .../generic-type-parameters-errors.md | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index 719b1a4ca130e..e2e7cea8b74fa 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -55,37 +55,37 @@ This article covers the following compiler errors: -- [**CS0080**](#type-parameter-declaration-and-naming): *Constraints are not allowed on non-generic declarations* -- [**CS0081**](#type-parameter-declaration-and-naming): *Type parameter declaration must be an identifier not a type* -- [**CS0224**](#type-argument-count-and-usage): *A method with vararg cannot be generic, be in a generic type, or have a params parameter* -- [**CS0304**](#constructor-constraints): *Cannot create an instance of the variable type 'type' because it does not have the new() constraint* -- [**CS0305**](#type-argument-count-and-usage): *Using the generic type 'generic type' requires 'number' type arguments* -- [**CS0306**](#type-argument-count-and-usage): *The type 'type' may not be used as a type argument* +- [**CS0080**](#type-parameter-declaration-and-naming): *Constraints are not allowed on non-generic declarations.* +- [**CS0081**](#type-parameter-declaration-and-naming): *Type parameter declaration must be an identifier not a type.* +- [**CS0224**](#type-argument-count-and-usage): *A method with vararg cannot be generic, be in a generic type, or have a params parameter.* +- [**CS0304**](#constructor-constraints): *Cannot create an instance of the variable type 'type' because it does not have the new() constraint.* +- [**CS0305**](#type-argument-count-and-usage): *Using the generic type 'generic type' requires 'number' type arguments.* +- [**CS0306**](#type-argument-count-and-usage): *The type 'type' may not be used as a type argument.* - [**CS0307**](#type-argument-count-and-usage): *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* - [**CS0308**](#type-argument-count-and-usage): *The non-generic type-or-method 'identifier' cannot be used with type arguments.* -- [**CS0310**](#constructor-constraints): *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'* +- [**CS0310**](#constructor-constraints): *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'.* - [**CS0311**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'T' in the generic type or method '\'. There is no implicit reference conversion from 'type1' to 'type2'.* - [**CS0312**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.* -- [**CS0313**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints.* +- [**CS0313**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types can not satisfy any interface constraints.* - [**CS0314**](#constraint-satisfaction-and-conversions): *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* -- [**CS0315**](#constraint-satisfaction-and-conversions): *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\\'. There is no boxing conversion from 'valueType' to 'referenceType'.* -- [**CS0403**](#generic-type-usage-restrictions): *Cannot use null as type argument for 'type parameter' because it may not be a reference type. Consider using 'default' instead.* -- [**CS0412**](#type-parameter-declaration-and-naming): *'parameter': a parameter, local variable, or local function cannot have the same name as a method type parameter* -- [**CS0413**](#generic-type-usage-restrictions): *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint* -- [**CS0417**](#constructor-constraints): *'identifier': cannot provide arguments when creating an instance of a variable type* -- [**CS0694**](#type-parameter-declaration-and-naming): *Type parameter 'identifier' has the same name as the containing type, or method* -- [**CS0695**](#generic-type-usage-restrictions): *Generic class cannot implement both 'generic interface' and 'generic interface' because they may unify for some type parameter substitutions* -- [**CS0698**](#generic-type-usage-restrictions): *A generic type cannot derive from 'type' because it is an attribute class* -- [**CS9338**](#generic-type-usage-restrictions): *Type argument 'type' must be public because it is used in a public signature.* +- [**CS0315**](#constraint-satisfaction-and-conversions): *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\'. There is no boxing conversion from 'valueType' to 'referenceType'.* +- [**CS0403**](#generic-type-usage-restrictions): *Cannot convert null to type parameter 'name' because it could be a non-nullable value type. Consider using 'default('T')' instead.* +- [**CS0412**](#type-parameter-declaration-and-naming): *'parameter': a parameter, local variable, or local function cannot have the same name as a method type parameter.* +- [**CS0413**](#generic-type-usage-restrictions): *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint.* +- [**CS0417**](#constructor-constraints): *'identifier': cannot provide arguments when creating an instance of a variable type.* +- [**CS0694**](#type-parameter-declaration-and-naming): *Type parameter 'identifier' has the same name as the containing type, or method.* +- [**CS0695**](#generic-type-usage-restrictions): *'type' cannot implement both 'interface1' and 'interface2' because they may unify for some type parameter substitutions.* +- [**CS0698**](#generic-type-usage-restrictions): *A generic type cannot derive from 'type' because it is an attribute class.* +- [**CS9338**](#generic-type-usage-restrictions): *Inconsistent accessibility: type 'type1' is less accessible than class 'type2'.* ## Type parameter declaration and naming The following errors relate to how type parameters are declared and named in generic types and methods: -- **CS0080**: *Constraints are not allowed on non-generic declarations* -- **CS0081**: *Type parameter declaration must be an identifier not a type* -- **CS0412**: *'generic': a parameter or local variable cannot have the same name as a method type parameter* -- **CS0694**: *Type parameter 'identifier' has the same name as the containing type, or method* +- **CS0080**: *Constraints are not allowed on non-generic declarations.* +- **CS0081**: *Type parameter declaration must be an identifier not a type.* +- **CS0412**: *'parameter': a parameter, local variable, or local function cannot have the same name as a method type parameter.* +- **CS0694**: *Type parameter 'identifier' has the same name as the containing type, or method.* To correct these errors, ensure that type parameters are declared with valid identifiers, constraint clauses are only applied to generic declarations, and type parameter names don't conflict with other identifiers in scope: @@ -136,7 +136,7 @@ The following errors relate to type arguments not satisfying the constraints of - **CS0311**: *The type 'type1' cannot be used as type parameter 'T' in the generic type or method '\'. There is no implicit reference conversion from 'type1' to 'type2'.* - **CS0312**: *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.* -- **CS0313**: *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints.* +- **CS0313**: *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types can not satisfy any interface constraints.* - **CS0314**: *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* - **CS0315**: *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\\'. There is no boxing conversion from 'valueType' to 'referenceType'.* @@ -153,18 +153,20 @@ For more information, see [Constraints on type parameters](../programming-guide/ The following errors relate to restrictions on how generic types can be used: -- **CS0403**: *Cannot use null as type argument for 'type parameter' because it may not be a reference type. Consider using 'default' instead.* -- **CS0413**: *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint* -- **CS0695**: *Generic class cannot implement both 'generic interface' and 'generic interface' because they may unify for some type parameter substitutions* -- **CS0698**: *A generic type cannot derive from 'type' because it is an attribute class* -- **CS9338**: *Type argument 'type' must be public because it is used in a public signature.* +- **CS0403**: *Cannot convert null to type parameter 'name' because it could be a non-nullable value type. Consider using 'default('T')' instead.* +- **CS0413**: *The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint.* +- **CS0695**: *'type' cannot implement both 'interface1' and 'interface2' because they may unify for some type parameter substitutions.* +- **CS0698**: *A generic type cannot derive from 'type' because it is an attribute class.* +- **CS9338**: *Inconsistent accessibility: type 'type1' is less accessible than class 'type2'.* To correct these errors, use `default` instead of `null` for unconstrained type parameters, add class constraints when using the `as` operator, avoid interface unification conflicts, don't create generic attribute classes, and ensure type arguments match the visibility of their containing members: - Replace `null` assignments with `default(T)` or add a class constraint (**CS0403**). When you assign `null` to an unconstrained type parameter, the compiler cannot guarantee that the type argument will be a reference type that accepts `null` values, because it might be a value type like `int` or `struct`, which cannot be `null`. To resolve this error, either use `default(T)` which provides the appropriate default value for any type (null for reference types, zero or empty for value types), or add a `class` constraint to the type parameter if you specifically need reference type semantics and want to allow `null` assignments. - Add a `class` or specific type constraint when using the `as` operator (**CS0413**). The `as` operator performs a safe type cast that returns `null` if the conversion fails, but this behavior is incompatible with value types because value types cannot be `null`. When you use `as` with an unconstrained type parameter, the compiler cannot guarantee the type argument won't be a value type, so it rejects the code. To fix this error, add a `class` constraint or a specific reference type constraint (like `where T : SomeClass`) to ensure the type parameter will always be a reference type that can properly handle the `null` result of a failed cast. - Avoid implementing the same generic interface multiple times with type parameters that could unify (**CS0695**). When a class implements a generic interface multiple times with different type parameters (such as `class G : I, I`), there's a risk that someone could instantiate it with the same type for both parameters (`G`), which would create a conflict because the class would effectively be implementing `I` twice. To resolve this error, either implement the interface only once, restructure your type parameters to prevent unification, or use separate non-generic classes for different specializations. -- Remove generic type parameters from attribute classes (**CS0698**). Attributes are metadata annotations that are stored in compiled assemblies and must be instantiated by the runtime's reflection system, which doesn't support generic attributes because of the complexity this would introduce in attribute resolution and application. If you need attribute-like behavior with type parameters, consider using factory patterns, creating separate non-generic attribute classes for each needed type, or redesigning your solution to avoid generic attributes entirely. +- Remove generic type parameters from attribute classes (**CS0698**). +> [!NOTE] +> This error is no longer produced in current versions of C#, as generic attributes are now supported. In earlier versions of C#, attributes were metadata annotations that were stored in compiled assemblies and had to be instantiated by the runtime's reflection system, which didn't support generic attributes because of the complexity this would introduce in attribute resolution and application. - Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). When you declare a public or protected member that uses a generic type with specific type arguments, those type arguments must themselves be publicly accessible, because otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but cannot properly work with it because they cannot see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../programming-guide/concepts/attributes/index.md). From 9085805efb22f1d1d34fee22bdb96488dc40bcdf Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 14:33:06 -0500 Subject: [PATCH 06/12] Final edit pass --- .../generic-type-parameters-errors.md | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index e2e7cea8b74fa..0b3e872d2da5b 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -90,8 +90,8 @@ The following errors relate to how type parameters are declared and named in gen To correct these errors, ensure that type parameters are declared with valid identifiers, constraint clauses are only applied to generic declarations, and type parameter names don't conflict with other identifiers in scope: - Remove the constraint clause from non-generic declarations (**CS0080**). The `where` clause can only be used on generic types and methods that declare type parameters, because constraints define requirements that type arguments must satisfy. If you need to apply constraints, first add type parameters to your type or method declaration. For example, change `public class MyClass where MyClass : System.IDisposable` to `public class MyClass where T : System.IDisposable`. -- Replace actual type names with identifiers in type parameter declarations (**CS0081**). Type parameters must be declared using identifiers (like `T`, `TKey`, or `TValue`) rather than concrete types (like `int` or `string`), because the purpose of a type parameter is to serve as a placeholder that will be substituted with actual types when the generic type or method is used. For example, change `public void F()` to `public void F()`. -- Rename type parameters, local variables, or parameters to avoid naming conflicts (**CS0412**, **CS0694**). Type parameter names must not shadow other identifiers in the same scope, and they must not match the name of the containing type or method, because such conflicts create ambiguity about which identifier is being referenced. For example, if you have a method `public void F()`, you cannot declare a local variable `double T` inside that method, and you cannot name a type parameter the same as its containing type (`class C`). +- Replace actual type names with identifiers in type parameter declarations (**CS0081**). Type parameters must be declared using identifiers (like `T`, `TKey`, or `TValue`) rather than concrete types (like `int` or `string`), because the purpose of a type parameter is to serve as a placeholder that is substituted with actual types when the generic type or method is used. For example, change `public void F()` to `public void F()`. +- Rename type parameters, local variables, or parameters to avoid naming conflicts (**CS0412**, **CS0694**). Type parameter names can't shadow identifiers in the same scope. They can't match the name of the containing type or method. Such conflicts create ambiguity about which identifier is being referenced. For example, if you have a method `public void F()`, you can't declare a local variable `double T` inside that method, and you can't name a type parameter the same as its containing type (`class C`). For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). @@ -105,12 +105,12 @@ The following errors relate to providing the correct number and type of type arg - **CS0307**: *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* - **CS0308**: *The non-generic type-or-method 'identifier' cannot be used with type arguments.* -To correct these errors, ensure that you provide the exact number of type arguments required by the generic declaration, use only valid types as type arguments, and don't apply type arguments to non-generic constructs: +To correct these errors, ensure that you provide the exact number of type arguments required by the generic declaration. Use only valid types as type arguments. Don't apply type arguments to non-generic constructs: - Remove generic type parameters or containing generic type declarations from methods that use `__arglist` (**CS0224**). The `__arglist` keyword is incompatible with generics because the runtime mechanisms for handling variable argument lists conflict with the type substitution required for generic type parameters. This restriction also applies to the `params` keyword when used in combination with generic methods or methods within generic types. -- Supply the exact number of type arguments specified in the generic type or method declaration (**CS0305**). Each generic type parameter declared in the definition must have a corresponding type argument when the generic type is instantiated, because the compiler needs to know which concrete type to substitute for each type parameter. For example, if a class is declared as `class MyList`, you must provide exactly one type argument when using it, such as `MyList`, not `MyList`. -- Use only valid types as type arguments (**CS0306**). Pointer types, such as `int*` or `char*`, cannot be used as type arguments because generic types require managed types that the garbage collector can track, and pointer types are unmanaged. If you need to work with pointers in a generic context, consider using `IntPtr` or restructuring your code to avoid mixing generics with unsafe code. -- Remove type argument syntax from non-generic constructs (**CS0307**, **CS0308**). Type arguments enclosed in angle brackets (like ``) can only be applied to generic types and methods that declare type parameters. If you attempt to use this syntax on properties, non-generic methods, or other non-generic constructs, you must either remove the type arguments entirely or ensure you have imported the correct namespace that contains the generic version of the type. For example, `IEnumerator` requires the `using System.Collections.Generic;` directive, whereas `IEnumerator` is in `System.Collections`. +- Supply the exact number of type arguments specified in the generic type or method declaration (**CS0305**). Each generic type parameter declared in the definition must have a corresponding type argument when the generic type is instantiated. The compiler needs to know which concrete type to substitute for each type parameter. For example, if a class is declared as `class MyList`, you must provide exactly one type argument when using it, such as `MyList`, not `MyList`. +- Use only valid types as type arguments (**CS0306**). Pointer types, such as `int*` or `char*`, can't be used as type arguments because generic types require managed types that the garbage collector can track, and pointer types are unmanaged. If you need to work with pointers in a generic context, consider using `IntPtr` or restructuring your code to avoid mixing generics with unsafe code. +- Remove type argument syntax from non-generic constructs (**CS0307**, **CS0308**). Type arguments enclosed in angle brackets (like ``) can only be applied to generic types and methods that declare type parameters. You must either remove the type arguments entirely or ensure you imported the namespace that contains the generic version of the type. For example, `IEnumerator` requires the `using System.Collections.Generic;` directive, whereas `IEnumerator` is in `System.Collections`. For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). @@ -124,9 +124,9 @@ The following errors relate to the `new()` constraint on generic type parameters To correct these errors, add the `new()` constraint to type parameters that need to be instantiated, ensure type arguments have public parameterless constructors, and avoid passing arguments when constructing instances of type parameters: -- Add the `new()` constraint to the type parameter declaration (**CS0304**). When you use the `new` operator to create an instance of a type parameter within a generic type or method, the compiler must be able to guarantee that any type argument supplied at runtime will have a parameterless constructor available. The `new()` constraint provides this guarantee at compile time, allowing the compiler to generate the appropriate instantiation code. For example, if you have `class C` with a member `T t = new T();`, you must change the declaration to `class C where T : new()`. -- Ensure that type arguments used with `new()` constrained type parameters have public parameterless constructors (**CS0310**). When a generic type or method declares a `new()` constraint on a type parameter, any concrete type used as a type argument must be non-abstract and must provide a public parameterless constructor. If a type only has non-public constructors (such as `private` or `protected`) or only has constructors with parameters, it cannot satisfy the `new()` constraint. To fix this error, either add a public parameterless constructor to the type, or use a different type argument that already has one. -- Remove constructor arguments when instantiating type parameters (**CS0417**). The `new()` constraint only guarantees the existence of a parameterless constructor, so attempting to pass arguments to `new T(arguments)` is not allowed because the compiler cannot verify that a constructor with those specific parameter types exists on the type that will eventually be substituted for `T`. If you need to construct instances with specific arguments, consider using factory methods, abstract factory patterns, or specific base class/interface constraints that define the construction behavior you need. +- Add the `new()` constraint to the type parameter declaration (**CS0304**). When you use the `new` operator to create an instance of a type parameter within a generic type or method, the compiler must be able to guarantee that any type argument supplied at runtime has a parameterless constructor available. The `new()` constraint provides this guarantee at compile time, allowing the compiler to generate the appropriate instantiation code. For example, if you have `class C` with a member `T t = new T();`, you must change the declaration to `class C where T : new()`. +- Ensure that type arguments used with `new()` constrained type parameters have public parameterless constructors (**CS0310**). When a generic type or method declares a `new()` constraint on a type parameter, any concrete type used as a type argument must be non-abstract and must provide a public parameterless constructor. If a type only has non-public constructors (such as `private` or `protected`) or only has constructors with parameters, it can't satisfy the `new()` constraint. To fix this error, either add a public parameterless constructor to the type, or use a different type argument that already has one. +- Remove constructor arguments when instantiating type parameters (**CS0417**). The `new()` constraint only guarantees the existence of a parameterless constructor, so attempting to pass arguments to `new T(arguments)` isn't allowed because the compiler can't verify that a constructor with those specific parameter types exists on the type that are substituted for `T`. If you need to construct instances with specific arguments, consider using factory methods, abstract factory patterns, or specific base class/interface constraints that define the construction behavior you need. For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md) and the [new() constraint](../keywords/new-constraint.md). @@ -138,14 +138,14 @@ The following errors relate to type arguments not satisfying the constraints of - **CS0312**: *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. The nullable type 'type1' does not satisfy the constraint of 'type2'.* - **CS0313**: *The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types can not satisfy any interface constraints.* - **CS0314**: *The type 'type1' cannot be used as type parameter 'name' in the generic type or method 'name'. There is no boxing conversion or type parameter conversion from 'type1' to 'type2'.* -- **CS0315**: *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\\'. There is no boxing conversion from 'valueType' to 'referenceType'.* +- **CS0315**: *The type 'valueType' cannot be used as type parameter 'T' in the generic type or method 'TypeorMethod\'. There is no boxing conversion from 'valueType' to 'referenceType'.* To correct these errors, use type arguments that satisfy all constraints through appropriate conversions, ensure derived classes repeat base class constraints, and understand that nullable value types have special constraint requirements: -- Change the type argument to one that has an implicit reference conversion to the constraint type (**CS0311**). When a type parameter has a constraint like `where T : BaseType`, any type argument must be convertible to `BaseType` through an implicit reference conversion or identity conversion. This means the type argument must either be `BaseType` itself, derive from `BaseType`, or implement `BaseType` if it's an interface. Note that implicit numeric conversions (such as from `short` to `int`) do not satisfy generic type parameter constraints because these are value conversions, not reference conversions. -- Repeat the base class's type parameter constraints in any derived class declaration (**CS0314**). When a derived generic class inherits from a base generic class that has constraints on its type parameters, the derived class must declare the same constraints on its corresponding type parameters. This repetition is required because the compiler needs to verify that type arguments supplied to the derived class will also satisfy the requirements of the base class. For example, if you have `public class A where T : SomeClass`, then any class deriving from it must be declared as `public class B : A where T : SomeClass`. -- Use non-nullable value types or change the constraint type (**CS0312**, **CS0313**). Nullable value types (such as `int?`) are distinct from their underlying value types and do not satisfy the same constraints. There is no implicit conversion between `int?` and `int`, and nullable value types cannot satisfy interface constraints because the nullable wrapper itself doesn't implement the interface, even though the underlying value type does. To fix these errors, either use the non-nullable form of the value type as the type argument, or adjust your constraint to accept `object` or a nullable reference type if appropriate. -- Ensure type arguments satisfy reference type or class constraints (**CS0315**). When a type parameter is constrained to a class type (such as `where T : SomeClass`), you cannot use a value type (struct) as the type argument because there is no boxing conversion that satisfies the constraint relationship. The constraint requires a reference type that has an inheritance or implementation relationship with the constraint type. To resolve this error, either change the struct to a class if semantically appropriate, or remove the class constraint if the generic type can work with value types. +- Change the type argument to one that has an implicit reference conversion to the constraint type (**CS0311**). When a type parameter has a constraint like `where T : BaseType`, any type argument must be convertible to `BaseType` through an implicit reference conversion or identity conversion. The type argument must either be `BaseType` itself, derive from `BaseType`, or implement `BaseType` if it's an interface. Implicit numeric conversions (such as from `short` to `int`) don't satisfy generic type parameter constraints because these conversions are value conversions, not reference conversions. +- Repeat the base class's type parameter constraints in any derived class declaration (**CS0314**). When a derived generic class inherits from a base generic class that has constraints on its type parameters, the derived class must declare the same constraints on its corresponding type parameters. Repetition is required because the compiler needs to verify that type arguments supplied to the derived class satisfies the requirements of the base class. For example, if you have `public class A where T : SomeClass`, then any class deriving from it must be declared as `public class B : A where T : SomeClass`. +- Use non-nullable value types or change the constraint type (**CS0312**, **CS0313**). Nullable value types (such as `int?`) are distinct from their underlying value types and don't satisfy the same constraints. There's no implicit conversion between `int?` and `int`, and nullable value types can't satisfy interface constraints because the nullable wrapper itself doesn't implement the interface, even though the underlying value type does. To fix these errors, either use the non-nullable form of the value type as the type argument, or adjust your constraint to accept `object` or a nullable reference type if appropriate. +- Ensure type arguments satisfy reference type or class constraints (**CS0315**). When a type parameter is constrained to a class type (such as `where T : SomeClass`), you can't use a value type (struct) as the type argument because there's no boxing conversion that satisfies the constraint relationship. The constraint requires a reference type that has an inheritance or implementation relationship with the constraint type. To resolve this error, either change the struct to a class if semantically appropriate, or remove the class constraint if the generic type can work with value types. For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md) and [Implicit conversions](../../standard/base-types/conversion-tables.md). @@ -161,12 +161,12 @@ The following errors relate to restrictions on how generic types can be used: To correct these errors, use `default` instead of `null` for unconstrained type parameters, add class constraints when using the `as` operator, avoid interface unification conflicts, don't create generic attribute classes, and ensure type arguments match the visibility of their containing members: -- Replace `null` assignments with `default(T)` or add a class constraint (**CS0403**). When you assign `null` to an unconstrained type parameter, the compiler cannot guarantee that the type argument will be a reference type that accepts `null` values, because it might be a value type like `int` or `struct`, which cannot be `null`. To resolve this error, either use `default(T)` which provides the appropriate default value for any type (null for reference types, zero or empty for value types), or add a `class` constraint to the type parameter if you specifically need reference type semantics and want to allow `null` assignments. -- Add a `class` or specific type constraint when using the `as` operator (**CS0413**). The `as` operator performs a safe type cast that returns `null` if the conversion fails, but this behavior is incompatible with value types because value types cannot be `null`. When you use `as` with an unconstrained type parameter, the compiler cannot guarantee the type argument won't be a value type, so it rejects the code. To fix this error, add a `class` constraint or a specific reference type constraint (like `where T : SomeClass`) to ensure the type parameter will always be a reference type that can properly handle the `null` result of a failed cast. +- Replace `null` assignments with `default(T)` or add a class constraint (**CS0403**). When you assign `null` to an unconstrained type parameter, the compiler can't guarantee that the type argument is a reference type that accepts `null` values, because it might be a value type like `int` or `struct`, which can't be `null`. To resolve this error, either use `default(T)`, which provides the appropriate default value for any type (null for reference types, zero or empty for value types), or add a `class` constraint to the type parameter if you specifically need reference type semantics and want to allow `null` assignments. +- Add a `class` or specific type constraint when using the `as` operator (**CS0413**). The `as` operator performs a safe type cast that returns `null` if the conversion fails, but this behavior is incompatible with value types because value types can't be `null`. When you use `as` with an unconstrained type parameter, the compiler can't guarantee the type argument isn't a value type, so it rejects the code. To fix this error, add a `class` constraint or a specific reference type constraint (like `where T : SomeClass`) to ensure the type parameter is always a reference type that can properly handle the `null` result of a failed cast. - Avoid implementing the same generic interface multiple times with type parameters that could unify (**CS0695**). When a class implements a generic interface multiple times with different type parameters (such as `class G : I, I`), there's a risk that someone could instantiate it with the same type for both parameters (`G`), which would create a conflict because the class would effectively be implementing `I` twice. To resolve this error, either implement the interface only once, restructure your type parameters to prevent unification, or use separate non-generic classes for different specializations. - Remove generic type parameters from attribute classes (**CS0698**). > [!NOTE] -> This error is no longer produced in current versions of C#, as generic attributes are now supported. In earlier versions of C#, attributes were metadata annotations that were stored in compiled assemblies and had to be instantiated by the runtime's reflection system, which didn't support generic attributes because of the complexity this would introduce in attribute resolution and application. -- Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). When you declare a public or protected member that uses a generic type with specific type arguments, those type arguments must themselves be publicly accessible, because otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but cannot properly work with it because they cannot see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. +> This error is no longer produced in current versions of C#, as generic attributes are now supported. +- Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). A public or protected generic member must use type arguments that are be publicly accessible. Otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but can't properly work with it because they can't see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../programming-guide/concepts/attributes/index.md). From b5aba58076f422f72a78db53ea05ff7a66510d0a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 14:50:39 -0500 Subject: [PATCH 07/12] Fix build issues. --- .../generic-type-parameters-errors.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index 0b3e872d2da5b..26b3d02e5a59f 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -93,7 +93,7 @@ To correct these errors, ensure that type parameters are declared with valid ide - Replace actual type names with identifiers in type parameter declarations (**CS0081**). Type parameters must be declared using identifiers (like `T`, `TKey`, or `TValue`) rather than concrete types (like `int` or `string`), because the purpose of a type parameter is to serve as a placeholder that is substituted with actual types when the generic type or method is used. For example, change `public void F()` to `public void F()`. - Rename type parameters, local variables, or parameters to avoid naming conflicts (**CS0412**, **CS0694**). Type parameter names can't shadow identifiers in the same scope. They can't match the name of the containing type or method. Such conflicts create ambiguity about which identifier is being referenced. For example, if you have a method `public void F()`, you can't declare a local variable `double T` inside that method, and you can't name a type parameter the same as its containing type (`class C`). -For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). +For more information, see [Generic Type Parameters](../../programming-guide/generics/generic-type-parameters.md) and [Generics](../../fundamentals/types/generics.md). ## Type argument count and usage @@ -112,7 +112,7 @@ To correct these errors, ensure that you provide the exact number of type argume - Use only valid types as type arguments (**CS0306**). Pointer types, such as `int*` or `char*`, can't be used as type arguments because generic types require managed types that the garbage collector can track, and pointer types are unmanaged. If you need to work with pointers in a generic context, consider using `IntPtr` or restructuring your code to avoid mixing generics with unsafe code. - Remove type argument syntax from non-generic constructs (**CS0307**, **CS0308**). Type arguments enclosed in angle brackets (like ``) can only be applied to generic types and methods that declare type parameters. You must either remove the type arguments entirely or ensure you imported the namespace that contains the generic version of the type. For example, `IEnumerator` requires the `using System.Collections.Generic;` directive, whereas `IEnumerator` is in `System.Collections`. -For more information, see [Generic Type Parameters](../programming-guide/generics/generic-type-parameters.md) and [Generics](../fundamentals/types/generics.md). +For more information, see [Generic Type Parameters](../../programming-guide/generics/generic-type-parameters.md) and [Generics](../../fundamentals/types/generics.md). ## Constructor constraints @@ -128,7 +128,7 @@ To correct these errors, add the `new()` constraint to type parameters that need - Ensure that type arguments used with `new()` constrained type parameters have public parameterless constructors (**CS0310**). When a generic type or method declares a `new()` constraint on a type parameter, any concrete type used as a type argument must be non-abstract and must provide a public parameterless constructor. If a type only has non-public constructors (such as `private` or `protected`) or only has constructors with parameters, it can't satisfy the `new()` constraint. To fix this error, either add a public parameterless constructor to the type, or use a different type argument that already has one. - Remove constructor arguments when instantiating type parameters (**CS0417**). The `new()` constraint only guarantees the existence of a parameterless constructor, so attempting to pass arguments to `new T(arguments)` isn't allowed because the compiler can't verify that a constructor with those specific parameter types exists on the type that are substituted for `T`. If you need to construct instances with specific arguments, consider using factory methods, abstract factory patterns, or specific base class/interface constraints that define the construction behavior you need. -For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md) and the [new() constraint](../keywords/new-constraint.md). +For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md) and the [new() constraint](../keywords/new-constraint.md). ## Constraint satisfaction and conversions @@ -147,7 +147,7 @@ To correct these errors, use type arguments that satisfy all constraints through - Use non-nullable value types or change the constraint type (**CS0312**, **CS0313**). Nullable value types (such as `int?`) are distinct from their underlying value types and don't satisfy the same constraints. There's no implicit conversion between `int?` and `int`, and nullable value types can't satisfy interface constraints because the nullable wrapper itself doesn't implement the interface, even though the underlying value type does. To fix these errors, either use the non-nullable form of the value type as the type argument, or adjust your constraint to accept `object` or a nullable reference type if appropriate. - Ensure type arguments satisfy reference type or class constraints (**CS0315**). When a type parameter is constrained to a class type (such as `where T : SomeClass`), you can't use a value type (struct) as the type argument because there's no boxing conversion that satisfies the constraint relationship. The constraint requires a reference type that has an inheritance or implementation relationship with the constraint type. To resolve this error, either change the struct to a class if semantically appropriate, or remove the class constraint if the generic type can work with value types. -For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md) and [Implicit conversions](../../standard/base-types/conversion-tables.md). +For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md) and [Implicit conversions](../../../standard/base-types/conversion-tables.md). ## Generic type usage restrictions @@ -165,8 +165,8 @@ To correct these errors, use `default` instead of `null` for unconstrained type - Add a `class` or specific type constraint when using the `as` operator (**CS0413**). The `as` operator performs a safe type cast that returns `null` if the conversion fails, but this behavior is incompatible with value types because value types can't be `null`. When you use `as` with an unconstrained type parameter, the compiler can't guarantee the type argument isn't a value type, so it rejects the code. To fix this error, add a `class` constraint or a specific reference type constraint (like `where T : SomeClass`) to ensure the type parameter is always a reference type that can properly handle the `null` result of a failed cast. - Avoid implementing the same generic interface multiple times with type parameters that could unify (**CS0695**). When a class implements a generic interface multiple times with different type parameters (such as `class G : I, I`), there's a risk that someone could instantiate it with the same type for both parameters (`G`), which would create a conflict because the class would effectively be implementing `I` twice. To resolve this error, either implement the interface only once, restructure your type parameters to prevent unification, or use separate non-generic classes for different specializations. - Remove generic type parameters from attribute classes (**CS0698**). -> [!NOTE] -> This error is no longer produced in current versions of C#, as generic attributes are now supported. + > [!NOTE] + > This error is no longer produced in current versions of C#, as generic attributes are now supported. - Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). A public or protected generic member must use type arguments that are be publicly accessible. Otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but can't properly work with it because they can't see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. -For more information, see [Constraints on type parameters](../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../programming-guide/concepts/attributes/index.md). +For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../../programming-guide/concepts/attributes/index.md). From dae946312bf3f379ad25f9782e35b3093c51bd82 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 14:55:50 -0500 Subject: [PATCH 08/12] one more bad link --- .../compiler-messages/generic-type-parameters-errors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index 26b3d02e5a59f..aefdef2d78352 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -169,4 +169,4 @@ To correct these errors, use `default` instead of `null` for unconstrained type > This error is no longer produced in current versions of C#, as generic attributes are now supported. - Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). A public or protected generic member must use type arguments that are be publicly accessible. Otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but can't properly work with it because they can't see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. -For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../../programming-guide/concepts/attributes/index.md). +For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../attributes/general.md). From afa16eee2f80b9f79380bbf1bb88b151b7f9f03e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 15:12:35 -0500 Subject: [PATCH 09/12] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../compiler-messages/generic-type-parameters-errors.md | 2 +- docs/csharp/language-reference/toc.yml | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index aefdef2d78352..cd8ea56fb968e 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -167,6 +167,6 @@ To correct these errors, use `default` instead of `null` for unconstrained type - Remove generic type parameters from attribute classes (**CS0698**). > [!NOTE] > This error is no longer produced in current versions of C#, as generic attributes are now supported. -- Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). A public or protected generic member must use type arguments that are be publicly accessible. Otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but can't properly work with it because they can't see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. +- Ensure type arguments used in public or protected signatures are at least as accessible as the member using them (**CS9338**). A public or protected generic member must use type arguments that are publicly accessible. Otherwise external code couldn't properly reference or use the member's signature. For example, if you have `public class Container` where `T` is an internal type, external assemblies can see the `Container` but can't properly work with it because they can't see `T`. To fix this error, either make the type argument public, or reduce the accessibility of the member using it to match the type argument's accessibility. For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md), [default value expressions](../operators/default.md), and [Attributes](../attributes/general.md). diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index f762872afb067..c183361da12fd 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -982,8 +982,6 @@ items: href: ../misc/cs0410.md - name: CS0411 href: ../misc/cs0411.md - - name: CS0412 - href: ../misc/cs0412.md - name: CS0418 href: ../misc/cs0418.md - name: CS0423 From 817bf067db5f7004192c7d22f72037e177994928 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 15:40:02 -0500 Subject: [PATCH 10/12] fixes from Copilot review --- .../generic-type-parameters-errors.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md index cd8ea56fb968e..6a3c82aeca30a 100644 --- a/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -4,6 +4,7 @@ description: These compiler errors and warnings indicate errors in generic type f1_keywords: - "CS0080" - "CS0081" + - "CS0224" - "CS0304" - "CS0305" - "CS0306" @@ -26,6 +27,7 @@ f1_keywords: helpviewer_keywords: - "CS0080" - "CS0081" + - "CS0224" - "CS0304" - "CS0305" - "CS0306" @@ -99,9 +101,9 @@ For more information, see [Generic Type Parameters](../../programming-guide/gene The following errors relate to providing the correct number and type of type arguments to generic types and methods: -- **CS0224**: *A method with vararg cannot be generic, be in a generic type, or have a params parameter* -- **CS0305**: *Using the generic type 'generic type' requires 'number' type arguments* -- **CS0306**: *The type 'type' may not be used as a type argument* +- **CS0224**: *A method with vararg cannot be generic, be in a generic type, or have a params parameter.* +- **CS0305**: *Using the generic type 'generic type' requires 'number' type arguments.* +- **CS0306**: *The type 'type' may not be used as a type argument.* - **CS0307**: *The 'construct' 'identifier' is not a generic method. If you intended an expression list, use parentheses around the < expression.* - **CS0308**: *The non-generic type-or-method 'identifier' cannot be used with type arguments.* @@ -118,9 +120,9 @@ For more information, see [Generic Type Parameters](../../programming-guide/gene The following errors relate to the `new()` constraint on generic type parameters: -- **CS0304**: *Cannot create an instance of the variable type 'type' because it does not have the new() constraint* -- **CS0310**: *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'* -- **CS0417**: *'identifier': cannot provide arguments when creating an instance of a variable type* +- **CS0304**: *Cannot create an instance of the variable type 'type' because it does not have the new() constraint.* +- **CS0310**: *The type 'typename' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'.* +- **CS0417**: *'identifier': cannot provide arguments when creating an instance of a variable type.* To correct these errors, add the `new()` constraint to type parameters that need to be instantiated, ensure type arguments have public parameterless constructors, and avoid passing arguments when constructing instances of type parameters: From 333b256efa336fe4075951e8b7238d89eba43a88 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 16:03:54 -0500 Subject: [PATCH 11/12] why copilit, why? --- ...n-t-have-specifics-on-this-csharp-error.md | 784 ------------------ 1 file changed, 784 deletions(-) diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 715a1ec10f917..a45e995823a6e 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -532,790 +532,6 @@ f1_keywords: - "CS9095" - "CS9096" - "CS9097" -# C# 14 errors begin here - - "CS9338" -helpviewer_keywords: - - "CS0190" - - "CS0257" - - "CS0595" - - "CS0847" - - "CS0856" - - "CS0857" - - "CS1066" - - "CS1072" - - "CS1073" - - "CS1636" - - "CS1669" - - "CS1734" - - "CS1735" - - "CS1745" - - "CS1747" - - "CS1748" - - "CS1752" - - "CS1754" - - "CS1756" - - "CS1757" - - "CS1758" - - "CS1759" - - "CS1761" - - "CS1764" - - "CS1766" - - "CS1767" - - "CS1768" - - "CS1769" - - "CS1770" - - "CS1773" - - "CS1774" - - "CS1960" - - "CS1961" - - "CS1982" - - "CS1996" - - "CS1997" - - "CS2038" - - "CS2039" - - "CS2040" - - "CS2041" - - "CS2042" - - "CS2043" - - "CS2044" - - "CS2045" - - "CS2046" - - "CS3028" - - "CS4001" - - "CS4003" - - "CS4005" - - "CS4006" - - "CS4007" - - "CS4010" - - "CS4011" - - "CS4012" - - "CS4015" - - "CS4016" - - "CS4017" - - "CS4018" - - "CS4019" - - "CS4020" - - "CS4021" - - "CS4022" - - "CS4023" - - "CS4024" - - "CS4025" - - "CS4026" - - "CS4027" - - "CS4028" - - "CS4029" - - "CS4030" - - "CS4031" - - "CS4034" - - "CS4036" - - "CS7002" - - "CS7006" - - "CS7012" - - "CS7013" - - "CS7015" - - "CS7016" - - "CS7017" - - "CS7018" - - "CS7019" - - "CS7020" - - "CS7021" - - "CS7022" - - "CS7024" - - "CS7025" - - "CS7026" - - "CS7027" - - "CS7028" - - "CS7029" - - "CS7030" - - "CS7032" - - "CS7033" - - "CS7034" - - "CS7035" - - "CS7038" - - "CS7041" - - "CS7042" - - "CS7043" - - "CS7045" - - "CS7048" - - "CS7049" - - "CS7050" - - "CS7051" - - "CS7052" - - "CS7054" - - "CS7055" - - "CS7056" - - "CS7057" - - "CS7058" - - "CS7059" - - "CS7061" - - "CS7064" - - "CS7065" - - "CS7066" - - "CS7070" - - "CS7080" - - "CS7081" - - "CS7082" - - "CS7084" - - "CS7086" - - "CS7087" - - "CS7088" - - "CS7089" - - "CS7090" - - "CS7091" - - "CS7092" - - "CS7093" - - "CS7094" - - "CS7095" - - "CS7096" - - "CS7098" - - "CS7099" - - "CS7100" - - "CS7101" - - "CS7102" - - "CS7103" - - "CS8001" - - "CS8002" - - "CS8003" - - "CS8004" - - "CS8005" - - "CS8006" - - "CS8007" - - "CS8008" - - "CS8009" - - "CS8010" - - "CS8011" - - "CS8012" - - "CS8013" - - "CS8014" - - "CS8015" - - "CS8016" - - "CS8017" - - "CS8018" - - "CS8020" - - "CS8021" - - "CS8027" - - "CS8028" - - "CS8029" - - "CS8030" - - "CS8031" - - "CS8032" - - "CS8033" - - "CS8034" - - "CS8035" - - "CS8036" - - "CS8040" - - "CS8050" - - "CS8051" - - "CS8053" - - "CS8055" - - "CS8057" - - "CS8058" - - "CS8070" - - "CS8076" - - "CS8077" - - "CS8078" - - "CS8079" - - "CS8080" - - "CS8081" - - "CS8082" - - "CS8084" - - "CS8086" - - "CS8087" - - "CS8088" - - "CS8089" - - "CS8092" - - "CS8093" - - "CS8094" - - "CS8095" - - "CS8096" - - "CS8099" - - "CS8100" - - "CS8101" - - "CS8106" - - "CS8107" - - "CS8108" - - "CS8109" - - "CS8110" - - "CS8111" - - "CS8112" - - "CS8113" - - "CS8114" - - "CS8115" - - "CS8116" - - "CS8117" - - "CS8118" - - "CS8119" - - "CS8120" - - "CS8121" - - "CS8122" - - "CS8123" - - "CS8124" - - "CS8125" - - "CS8126" - - "CS8127" - - "CS8128" - - "CS8129" - - "CS8130" - - "CS8131" - - "CS8132" - - "CS8133" - - "CS8134" - - "CS8135" - - "CS8136" - - "CS8137" - - "CS8138" - - "CS8139" - - "CS8140" - - "CS8141" - - "CS8142" - - "CS8143" - - "CS8144" - - "CS8145" - - "CS8146" - - "CS8147" - - "CS8148" - - "CS8149" - - "CS8150" - - "CS8151" - - "CS8153" - - "CS8154" - - "CS8155" - - "CS8156" - - "CS8157" - - "CS8158" - - "CS8159" - - "CS8160" - - "CS8161" - - "CS8162" - - "CS8163" - - "CS8164" - - "CS8165" - - "CS8166" - - "CS8167" - - "CS8168" - - "CS8169" - - "CS8170" - - "CS8171" - - "CS8172" - - "CS8173" - - "CS8174" - - "CS8175" - - "CS8176" - - "CS8177" - - "CS8178" - - "CS8179" - - "CS8180" - - "CS8181" - - "CS8182" - - "CS8183" - - "CS8184" - - "CS8185" - - "CS8186" - - "CS8187" - - "CS8188" - - "CS8189" - - "CS8190" - - "CS8191" - - "CS8192" - - "CS8193" - - "CS8194" - - "CS8195" - - "CS8196" - - "CS8197" - - "CS8198" - - "CS8199" - - "CS8200" - - "CS8201" - - "CS8202" - - "CS8203" - - "CS8204" - - "CS8205" - - "CS8206" - - "CS8207" - - "CS8208" - - "CS8209" - - "CS8210" - - "CS8211" - - "CS8212" - - "CS8213" - - "CS8214" - - "CS8215" - - "CS8216" - - "CS8217" - - "CS8218" - - "CS8219" - - "CS8220" - - "CS8221" - - "CS8222" - - "CS8223" - - "CS8224" - - "CS8225" - - "CS8226" - - "CS8227" - - "CS8228" - - "CS8229" - - "CS8230" - - "CS8231" - - "CS8232" - - "CS8233" - - "CS8234" - - "CS8235" - - "CS8236" - - "CS8237" - - "CS8238" - - "CS8239" - - "CS8240" - - "CS8241" - - "CS8242" - - "CS8243" - - "CS8244" - - "CS8245" - - "CS8246" - - "CS8247" - - "CS8248" - - "CS8249" - - "CS8250" - - "CS8251" - - "CS8252" - - "CS8253" - - "CS8254" - - "CS8255" - - "CS8256" - - "CS8257" - - "CS8258" - - "CS8259" - - "CS8260" - - "CS8261" - - "CS8262" - - "CS8263" - - "CS8264" - - "CS8265" - - "CS8266" - - "CS8267" - - "CS8268" - - "CS8269" - - "CS8270" - - "CS8271" - - "CS8272" - - "CS8273" - - "CS8274" - - "CS8275" - - "CS8276" - - "CS8277" - - "CS8278" - - "CS8279" - - "CS8280" - - "CS8281" - - "CS8282" - - "CS8283" - - "CS8284" - - "CS8285" - - "CS8286" - - "CS8287" - - "CS8288" - - "CS8289" - - "CS8290" - - "CS8291" - - "CS8292" - - "CS8293" - - "CS8294" - - "CS8295" - - "CS8296" - - "CS8297" - - "CS8298" - - "CS8299" - - "CS8300" - - "CS8301" - - "CS8302" - - "CS8303" - - "CS8304" - - "CS8305" - - "CS8306" - - "CS8307" - - "CS8308" - - "CS8309" - - "CS8310" - - "CS8311" - - "CS8312" - - "CS8313" - - "CS8314" - - "CS8315" - - "CS8316" - - "CS8317" - - "CS8318" - - "CS8319" - - "CS8320" - - "CS8321" - - "CS8322" - - "CS8323" - - "CS8324" - - "CS8325" - - "CS8326" - - "CS8327" - - "CS8328" - - "CS8329" - - "CS8330" - - "CS8331" - - "CS8332" - - "CS8333" - - "CS8334" - - "CS8335" - - "CS8336" - - "CS8337" - - "CS8338" - - "CS8339" - - "CS8340" - - "CS8341" - - "CS8342" - - "CS8343" - - "CS8344" - - "CS8345" - - "CS8346" - - "CS8347" - - "CS8348" - - "CS8349" - - "CS8350" - - "CS8351" - - "CS8352" - - "CS8353" - - "CS8354" - - "CS8355" - - "CS8356" - - "CS8357" - - "CS8358" - - "CS8359" - - "CS8360" - - "CS8361" - - "CS8362" - - "CS8363" - - "CS8364" - - "CS8365" - - "CS8366" - - "CS8367" - - "CS8368" - - "CS8369" - - "CS8370" - - "CS8371" - - "CS8372" - - "CS8373" - - "CS8374" - - "CS8375" - - "CS8376" - - "CS8377" - - "CS8378" - - "CS8379" - - "CS8380" - - "CS8381" - - "CS8382" - - "CS8383" - - "CS8384" - - "CS8385" - - "CS8386" - - "CS8387" - - "CS8388" - - "CS8389" - - "CS8390" - - "CS8391" - - "CS8392" - - "CS8393" - - "CS8394" - - "CS8395" - - "CS8396" - - "CS8397" - - "CS8398" - - "CS8399" - - "CS8400" - - "CS8401" - - "CS8402" - - "CS8403" - - "CS8404" - - "CS8405" - - "CS8406" - - "CS8407" - - "CS8408" - - "CS8409" - - "CS8410" - - "CS8411" - - "CS8412" - - "CS8413" - - "CS8414" - - "CS8415" - - "CS8416" - - "CS8417" - - "CS8418" - - "CS8419" - - "CS8420" - - "CS8421" - - "CS8422" - - "CS8423" - - "CS8424" - - "CS8425" - - "CS8426" - - "CS8427" - - "CS8428" - - "CS8429" - - "CS8502" - - "CS8503" - - "CS8504" - - "CS8505" - - "CS8506" - - "CS8508" - - "CS8510" - - "CS8512" - - "CS8513" - - "CS8516" - - "CS8517" - - "CS8518" - - "CS8519" - - "CS8520" - - "CS8521" - - "CS8522" - - "CS8523" - - "CS8524" - - "CS8635" - - "CS8641" - - "CS8646" - - "CS8650" - - "CS8651" - - "CS8656" - - "CS8657" - - "CS8658" - - "CS8659" - - "CS8660" - - "CS8661" - - "CS8662" - - "CS8664" - - "CS8665" - - "CS8666" - - "CS8669" - - "CS8700" - - "CS8701" - - "CS8702" - - "CS8707" - - "CS8712" - - "CS8715" - - "CS8716" - - "CS8750" - - "CS8751" - - "CS8752" - - "CS8753" - - "CS8754" - - "CS8755" - - "CS8756" - - "CS8757" - - "CS8758" - - "CS8759" - - "CS8760" - - "CS8761" - - "CS8762" - - "CS8763" - - "CS8764" - - "CS8765" - - "CS8766" - - "CS8800" - - "CS8801" - - "CS8802" - - "CS8803" - - "CS8804" - - "CS8805" - - "CS8806" - - "CS8900" - - "CS8901" - - "CS8902" - - "CS8903" - - "CS8904" - - "CS8905" - - "CS8906" - - "CS8907" - - "CS8908" - - "CS8909" - - "CS8910" - - "CS8911" - - "CS8912" - - "CS8913" - - "CS8914" - - "CS8915" - - "CS8916" - - "CS8917" - - "CS8918" - - "CS8919" - - "CS8920" - - "CS8921" - - "CS8922" - - "CS8923" - - "CS8924" - - "CS8925" - - "CS8926" - - "CS8927" - - "CS8928" - - "CS8929" - - "CS8930" - - "CS8931" - - "CS8932" - - "CS8933" - - "CS8934" - - "CS8935" - - "CS8936" - - "CS8937" - - "CS8938" - - "CS8939" - - "CS8940" - - "CS8941" - - "CS8942" - - "CS8943" - - "CS8944" - - "CS8945" - - "CS8946" - - "CS8947" - - "CS8948" - - "CS8949" - - "CS8950" - - "CS8951" - - "CS8952" - - "CS8953" - - "CS8955" - - "CS8956" - - "CS8957" - - "CS8958" - - "CS8959" - - "CS8960" - - "CS8961" - - "CS8962" - - "CS8963" - - "CS8964" - - "CS8965" - - "CS8966" - - "CS8967" - - "CS8968" - - "CS8969" - - "CS8970" - - "CS8971" - - "CS8972" - - "CS8973" - - "CS8974" - - "CS8975" - - "CS8976" - - "CS8977" - - "CS8978" - - "CS8979" - - "CS8980" - - "CS8981" - - "CS8982" - - "CS8983" - - "CS8984" - - "CS8985" - - "CS8986" - - "CS8987" - - "CS8988" - - "CS8989" - - "CS8990" - - "CS8991" - - "CS8992" - - "CS8993" - - "CS8994" - - "CS8995" - - "CS8996" - - "CS8997" - - "CS8998" - - "CS8999" - - "CS9000" - - "CS9001" - - "CS9002" - - "CS9003" - - "CS9004" - - "CS9005" - - "CS9006" - - "CS9007" - - "CS9008" - - "CS9009" - - "CS9010" - - "CS9011" - - "CS9012" - - "CS9013" - - "CS9014" - - "CS9015" - - "CS9016" - - "CS9017" - - "CS9018" - - "CS9019" - - "CS9020" - - "CS9021" - - "CS9022" - - "CS9023" - - "CS9024" - - "CS9025" - - "CS9026" - - "CS9027" - - "CS9028" - - "CS9029" - - "CS9030" - - "CS9031" - - "CS9032" - - "CS9033" - - "CS9034" - - "CS9035" - - "CS9036" - - "CS9037" - - "CS9038" - - "CS9039" - - "CS9040" - - "CS9041" - - "CS9042" - - "CS9043" - - "CS9044" - - "CS9045" - - "CS9046" - - "CS9047" - - "CS9048" - - "CS9049" - - "CS9050" - - "CS9051" - - "CS9052" - - "CS9053" - - "CS9054" - - "CS9055" - - "CS9056" - - "CS9057" - - "CS9058" - - "CS9059" - - "CS9060" - - "CS9061" - - "CS9062" - - "CS9063" - - "CS9065" - - "CS9066" - - "CS9067" - - "CS9068" - - "CS9069" - - "CS9070" - - "CS9071" - - "CS9073" - - "CS9074" - - "CS9075" - - "CS9076" - - "CS9077" - - "CS9078" - - "CS9079" - - "CS9080" - - "CS9081" - - "CS9082" - - "CS9083" - - "CS9084" - - "CS9085" - - "CS9086" - - "CS9087" - - "CS9088" - - "CS9089" - - "CS9090" - - "CS9091" - - "CS9092" - - "CS9093" - - "CS9094" - - "CS9095" - - "CS9096" - - "CS9097" - - "CS9338" - - "errors [C#], additional information" --- # Sorry, we don't have specifics on this C# error From b6109dfaecf00484a79459b1b0679c4d0b4ea0c6 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 Nov 2025 16:32:06 -0500 Subject: [PATCH 12/12] revert --- .../misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index a45e995823a6e..15c2303696612 100644 --- a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md +++ b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md @@ -532,6 +532,8 @@ f1_keywords: - "CS9095" - "CS9096" - "CS9097" +helpviewer_keywords: + - "errors [C#], additional information" --- # Sorry, we don't have specifics on this C# error