diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 43a9e9da2f695..aa1d4db2f7c56 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. @@ -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/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index c773c4e821a7b..45dc2b1d642b0 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -1,5 +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 new file mode 100644 index 0000000000000..6a3c82aeca30a --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/generic-type-parameters-errors.md @@ -0,0 +1,174 @@ +--- +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" + - "CS0224" + - "CS0304" + - "CS0305" + - "CS0306" + - "CS0307" + - "CS0308" + - "CS0310" + - "CS0311" + - "CS0312" + - "CS0313" + - "CS0314" + - "CS0315" + - "CS0403" + - "CS0412" + - "CS0413" + - "CS0417" + - "CS0694" + - "CS0695" + - "CS0698" + - "CS9338" +helpviewer_keywords: + - "CS0080" + - "CS0081" + - "CS0224" + - "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 +--- +# Resolve errors and warnings related to generic type parameters and generic type arguments + +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.* +- [**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 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 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**: *'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: + +- 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 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). + +## 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.* + +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. 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). + +## 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.* + +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 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). + +## Constraint satisfaction and conversions + +The following errors relate to type arguments not satisfying the constraints of generic type parameters: + +- **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 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'.* + +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. 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). + +## Generic type usage restrictions + +The following errors relate to restrictions on how generic types can be used: + +- **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 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. +- 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 74f177bc16a86..c183361da12fd 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -509,6 +509,13 @@ 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, + 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: > @@ -773,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 @@ -963,34 +966,10 @@ 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 - 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 @@ -1003,12 +982,6 @@ items: href: ../misc/cs0410.md - name: CS0411 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..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 @@ -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" @@ -533,8 +532,6 @@ f1_keywords: - "CS9095" - "CS9096" - "CS9097" -# C# 14 errors begin here - - "CS9338" helpviewer_keywords: - "errors [C#], additional information" ---