diff --git a/.github/instructions/Markdown.WritingStyle.instructions.md b/.github/instructions/Markdown.WritingStyle.instructions.md index 2857d9d7fcaeb..aae5e7b106622 100644 --- a/.github/instructions/Markdown.WritingStyle.instructions.md +++ b/.github/instructions/Markdown.WritingStyle.instructions.md @@ -41,7 +41,7 @@ ALWAYS: - **MANDATORY: Number ordered lists using "1." for every item (NOT 1., 2., 3.) - ALWAYS USE "1."** - **REQUIRED: Use bullets for unordered lists - NEVER use numbers for unordered content** - **ESSENTIAL: Write complete sentences in lists with proper punctuation** -- **MUST: End list items with periods if more than three words - THIS IS NON-NEGOTIABLE** +- **MUST: End list items with periods if more than three words. The period may be inside style marks like `*` for italic or `**` for bold - THIS IS NON-NEGOTIABLE** - Skip end punctuation on titles, headings, and UI elements (3 words or fewer) ### Spacing and Layout diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index c96c2f381d53d..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 `attribute-usage-errors.md`. -- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to declaring and using *attributes*. +- 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,5 +64,16 @@ 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 + +For every line in this table, verify that the error message associated with this error code matches the verbatim text in CSharpResources.resx. You can find the mapping using ErrorCodes.cs: + +1. Find that number as a constant in `ErrorCodes.cs`. +2. Locate the corresponding `data` element in CSharpResources.resx. The `name` atttribute should match the number of the constant. +3. Read the error message found in the `` element that is a child of that `` element. + + + +Now, ensure every instance of an error message matches the text in the highlighted table. \ No newline at end of file diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index a8867a28f0d8f..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" @@ -315,6 +415,18 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0034.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0071.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0106.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0277.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0178.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -355,6 +467,22 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0650.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0686.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0736.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0737.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0738.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0767.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -1833,10 +1961,34 @@ "source_path_from_root": "/docs/csharp/misc/cs0457.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0425.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0460.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0466.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0470.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0473.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0531.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0535.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0501.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-declarations" @@ -1865,6 +2017,30 @@ "source_path_from_root": "/docs/csharp/misc/cs0526.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/constructor-errors#constructor-declarations" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0538.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0539.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0540.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0541.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0550.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0551.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0553.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" diff --git a/docs/core/testing/unit-testing-mstest-sdk.md b/docs/core/testing/unit-testing-mstest-sdk.md index 713f8283a2762..df8566a175799 100644 --- a/docs/core/testing/unit-testing-mstest-sdk.md +++ b/docs/core/testing/unit-testing-mstest-sdk.md @@ -91,7 +91,6 @@ You can set the profile using the property `TestingExtensionsProfile` with one o Enables the following extensions: * [Code Coverage](./microsoft-testing-platform-extensions-code-coverage.md#microsoft-code-coverage) - * [Trx Report](./microsoft-testing-platform-extensions-test-reports.md#visual-studio-test-reports) * `AllMicrosoft` - Enable all extensions shipped by Microsoft (including extensions with a restrictive license). @@ -99,17 +98,11 @@ You can set the profile using the property `TestingExtensionsProfile` with one o Enables the following extensions: * [Code Coverage](./microsoft-testing-platform-extensions-code-coverage.md#microsoft-code-coverage) - * [Crash Dump](./microsoft-testing-platform-extensions-diagnostics.md#crash-dump) - * [Fakes](./microsoft-testing-platform-extensions-fakes.md#fakes-extension) (MSTest.Sdk 3.7.0+) - * [Hang Dump](./microsoft-testing-platform-extensions-diagnostics.md#hang-dump) - * [Hot Reload](./microsoft-testing-platform-extensions-hosting.md#hot-reload) - * [Retry](./microsoft-testing-platform-extensions-policy.md#retry) - * [Trx Report](./microsoft-testing-platform-extensions-test-reports.md#visual-studio-test-reports) Here's a full example, using the `None` profile: @@ -129,12 +122,14 @@ Here's a full example, using the `None` profile: |-------------------------------------------------------------------------------------------|:----:|:------------------:|:--------------------------------------:| | [Code Coverage](https://www.nuget.org/packages/Microsoft.Testing.Extensions.CodeCoverage) | | :heavy_check_mark: | :heavy_check_mark: | | [Crash Dump](https://www.nuget.org/packages/Microsoft.Testing.Extensions.CrashDump) | | | :heavy_check_mark: | -| [Fakes](https://www.nuget.org/packages/Microsoft.Testing.Extensions.Fakes) | | | :heavy_check_mark: (MSTest.Sdk 3.7.0+) | +| [Fakes](https://www.nuget.org/packages/Microsoft.Testing.Extensions.Fakes) | | | :heavy_check_mark:† | | [Hang Dump](https://www.nuget.org/packages/Microsoft.Testing.Extensions.HangDump) | | | :heavy_check_mark: | | [Hot Reload](https://www.nuget.org/packages/Microsoft.Testing.Extensions.HotReload) | | | :heavy_check_mark: | | [Retry](https://www.nuget.org/packages/Microsoft.Testing.Extensions.Retry) | | | :heavy_check_mark: | | [Trx](https://www.nuget.org/packages/Microsoft.Testing.Extensions.TrxReport) | | :heavy_check_mark: | :heavy_check_mark: | +† MSTest.Sdk 3.7.0+ + ### Enable or disable extensions Extensions can be enabled and disabled by MSBuild properties with the pattern `Enable[NugetPackageNameWithoutDots]`. @@ -183,7 +178,7 @@ Outside of the selection of the runner and runner-specific extensions, `MSTest.S Aspire is an opinionated, cloud-ready stack for building observable, production ready, distributed applications. Aspire is delivered through a collection of NuGet packages that handle specific cloud-native concerns. For more information, see the [Aspire docs](/dotnet/aspire/get-started/aspire-overview). > [!NOTE] -> This feature is available from MSTest.Sdk 3.4.0 +> This feature is available from MSTest.Sdk 3.4.0. By setting the property `EnableAspireTesting` to `true`, you can bring all dependencies and default `using` directives you need for testing with `Aspire` and `MSTest`. @@ -203,7 +198,7 @@ By setting the property `EnableAspireTesting` to `true`, you can bring all depen Playwright enables reliable end-to-end testing for modern web apps. For more information, see the official [Playwright docs](https://playwright.dev/dotnet/docs/intro). > [!NOTE] -> This feature is available from MSTest.Sdk 3.4.0 +> This feature is available from MSTest.Sdk 3.4.0. By setting the property `EnablePlaywright` to `true` you can bring in all the dependencies and default `using` directives you need for testing with `Playwright` and `MSTest`. @@ -268,7 +263,7 @@ Finally, based on the extensions profile you're using, you can also remove some Once you've updated your projects, if you're using `Microsoft.Testing.Platform` (default) and if you rely on `dotnet test` to run your tests, you must update your CI configuration. For more information and to guide your understanding of all the required changes, see [dotnet test integration](./unit-testing-with-dotnet-test.md). -If you are using the VSTest mode of `dotnet test`, here's an example update when using the `DotNetCoreCLI` task in Azure DevOps: +If you're using the VSTest mode of `dotnet test`, here's an example update when using the `DotNetCoreCLI` task in Azure DevOps: ```diff \- task: DotNetCoreCLI@2 @@ -281,11 +276,7 @@ If you are using the VSTest mode of `dotnet test`, here's an example update when ## Known limitations -The NuGet-provided MSBuild SDKs (including MSTest.Sdk) have limited tooling support when it comes to updating its version, meaning that the usual NuGet update and Visual Studio UI for managing NuGet packages does not work as expected. See this issue for more details: [NuGet#13127](https://github.com/NuGet/Home/issues/13127). - -> [!NOTE] -> This limitation is not specific to MSTest SDK but to any NuGet-provided MSBuild SDK. -> Dependabot will handle updating the version in the `global.json` file, but you will need to [manually update the version in the project file](https://github.com/dependabot/dependabot-core/issues/8615). +The NuGet-provided MSBuild SDKs (including MSTest.Sdk) have [limited tooling support](https://github.com/NuGet/Home/issues/13127) when it comes to updating their version, meaning that the usual NuGet update and Visual Studio UI for managing NuGet packages doesn't work as expected. You'll need to manually update the version in the `global.json` file and in the project file. (This applies even if you use Dependabot due to issues [dependabot-core#12824](https://github.com/dependabot/dependabot-core/issues/12824) and [dependabot-core#8615](https://github.com/dependabot/dependabot-core/issues/8615).) ## See also diff --git a/docs/csharp/language-reference/compiler-messages/cs0071.md b/docs/csharp/language-reference/compiler-messages/cs0071.md deleted file mode 100644 index 38b42d0353303..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0071.md +++ /dev/null @@ -1,52 +0,0 @@ ---- -description: "Compiler Error CS0071" -title: "Compiler Error CS0071" -ms.date: 07/20/2015 -f1_keywords: - - "CS0071" -helpviewer_keywords: - - "CS0071" -ms.assetid: 787cbeae-fb2b-455a-ba10-811b956ed170 ---- -# Compiler Error CS0071 - -An explicit interface implementation of an event must use event accessor syntax - -When explicitly implementing an [event](../keywords/event.md) that was declared in an interface, you must manually provide the `add` and `remove` event accessors that are typically provided by the compiler. The accessor code can connect the interface event to another event in your class (shown later in this topic) or to its own delegate type. For more information, see [How to implement interface events](../../programming-guide/events/how-to-implement-interface-events.md). - -## Example - - The following sample generates CS0071. - -```csharp -// CS0071.cs -public delegate void MyEvent(object sender); - -interface ITest -{ - event MyEvent Clicked; -} - -class Test : ITest -{ - event MyEvent ITest.Clicked; // CS0071 - - // Try the following code instead. - /* - private MyEvent clicked; - - event MyEvent ITest.Clicked - { - add - { - clicked += value; - } - remove - { - clicked -= value; - } - } - */ - public static void Main() { } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs0106.md b/docs/csharp/language-reference/compiler-messages/cs0106.md deleted file mode 100644 index f73e0b8c30380..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0106.md +++ /dev/null @@ -1,70 +0,0 @@ ---- -description: "Compiler Error CS0106" -title: "Compiler Error CS0106" -ms.date: 06/15/2017 -f1_keywords: - - "CS0106" -helpviewer_keywords: - - "CS0106" -ms.assetid: 8dec906a-ed69-4ed5-aa61-c8600d138200 ---- -# Compiler Error CS0106 - -The modifier 'modifier' is not valid for this item - - A class or interface member was marked with an invalid access modifier. The following examples describe some of these invalid modifiers: - -- The [static](../keywords/static.md) modifier is not permitted on a [local function](../../programming-guide/classes-and-structs/local-functions.md). The static local function feature is supported starting with C# 8.0. A compiler that doesn't support C# 8.0 produces CS0106 when you try to use this feature. However, a compiler that supports C# 8.0 but the set language version is prior to C# 8.0 will produce a diagnostic suggesting that you use C# 8.0 or later. - -- The `public` keyword is not allowed on an explicit interface declaration. In this case, remove the `public` keyword from the explicit interface declaration. - -- The [abstract](../keywords/abstract.md) keyword is not allowed on an explicit interface declaration because an explicit interface implementation can never be overridden. - -- Access modifiers are not allowed on a [local function](../../programming-guide/classes-and-structs/local-functions.md). Local functions are always private. - -- The [readonly](../keywords/readonly.md) keyword is not allowed on methods in a class type, with the exception of `ref readonly` returns (`readonly` keyword must appear after the `ref` keyword). - - In prior releases of Visual Studio, the `static` modifier was not permitted on a class, but `static` classes are allowed starting with Visual Studio 2005. - - For more information, see [Interfaces](../../fundamentals/types/interfaces.md). - -## Example - - The following sample generates CS0106: - -```csharp -// CS0106.cs -namespace MyNamespace -{ - interface I - { - void M1(); - void M2(); - } - - public class MyClass : I - { - public readonly int Prop1 { get; set; } // CS0106 - public int Prop2 { get; readonly set; } // CS0106 - - public void I.M1() {} // CS0106 - abstract void I.M2() {} // CS0106 - - public void AccessModifierOnLocalFunction() - { - public void LocalFunction() {} // CS0106 - } - - public readonly void ReadonlyMethod() {} // CS0106 - // Move the `readonly` keyword after the `ref` keyword - public readonly ref int ReadonlyBeforeRef(ref int reference) // CS0106 - { - return ref reference; - } - - public static void Main() {} - } - - public readonly class ReadonlyClass {} // CS0106 -} -``` 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/cs0686.md b/docs/csharp/language-reference/compiler-messages/cs0686.md deleted file mode 100644 index 8d62d54acf6ae..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0686.md +++ /dev/null @@ -1,76 +0,0 @@ ---- -description: "Compiler Error CS0686" -title: "Compiler Error CS0686" -ms.date: 07/20/2015 -f1_keywords: - - "CS0686" -helpviewer_keywords: - - "CS0686" -ms.assetid: 821ea0c9-87d8-4902-8f0b-dcad72312132 ---- -# Compiler Error CS0686 - -Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation. - - Suggested: This error can occur when implementing an interface that contains method names which conflict with the auto-generated methods associated with a property or event. The get/set methods for properties are generated as get_property and set_property, and the add/remove methods for events are generated as add_event and remove_event. If an interface contains either of these methods, a conflict occurs. To avoid this error, implement the methods using an explicit interface implementation. To do this, specify the function as: - -```csharp -Interface.get_property() { /* */ } -Interface.set_property() { /* */ } -``` - -## Example 1 - - The following sample generates CS0686: - -```csharp -// CS0686.cs -interface I -{ - int get_P(); -} - -class C : I -{ - public int P - { - get { return 1; } // CS0686 - } -} -// But the following is valid: -class D : I -{ - int I.get_P() { return 1; } - public static void Main() {} -} -``` - -## Example 2 - - This error can also occur when declaring events. The event construct automatically generates the `add_event` and `remove_event` methods, which could conflict with the methods of the same name in an interface, as in the following sample: - -```csharp -// CS0686b.cs -using System; - -interface I -{ - void add_OnMyEvent(EventHandler e); -} - -class C : I -{ - public event EventHandler OnMyEvent - { - add { } // CS0686 - remove { } - } -} - -// Correct (using explicit interface implementation): -class D : I -{ - void I.add_OnMyEvent(EventHandler e) {} - public static void Main() {} -} -``` 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/compiler-messages/interface-implementation-errors.md b/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md new file mode 100644 index 0000000000000..aad25e16b3e2e --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md @@ -0,0 +1,229 @@ +--- +title: Resolve errors and warnings related to interface implementation. +description: These compiler errors and warnings indicate errors in declaring methods that implement an interface member. +f1_keywords: + - "CS0071" + - "CS0106" + - "CS0277" + - "CS0425" + - "CS0460" + - "CS0470" + - "CS0473" + - "CS0531" + - "CS0535" + - "CS0538" + - "CS0539" + - "CS0540" + - "CS0541" + - "CS0550" + - "CS0551" + - "CS0630" + - "CS0686" + - "CS0736" + - "CS0737" + - "CS0738" + - "CS8705" + - "CS8854" + - "CS9333" + - "CS9334" +helpviewer_keywords: + - "CS0071" + - "CS0106" + - "CS0277" + - "CS0425" + - "CS0460" + - "CS0470" + - "CS0473" + - "CS0531" + - "CS0535" + - "CS0538" + - "CS0539" + - "CS0540" + - "CS0541" + - "CS0550" + - "CS0551" + - "CS0630" + - "CS0686" + - "CS0736" + - "CS0737" + - "CS0738" + - "CS8705" + - "CS8854" + - "CS9333" + - "CS9334" +ms.date: 11/12/2025 +ai-usage: ai-assisted +--- +# Resolve errors and warnings related to members that implement an interface + +This article covers the following compiler errors: + + +- [**CS0071**](#interface-declaration-and-syntax): *An explicit interface implementation of an event must use event accessor syntax.* +- [**CS0106**](#interface-declaration-and-syntax): *The modifier is not valid for this item.* +- [**CS0277**](#accessor-implementation-and-conflicts): *Member does not implement interface member because it is not public.* +- [**CS0425**](#generic-type-constraints): *The constraints for type parameter of method must match the constraints for type parameter of interface method. Consider using an explicit interface implementation instead.* +- [**CS0460**](#generic-type-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly, except for either a 'class', or a 'struct' constraint.* +- [**CS0470**](#accessor-implementation-and-conflicts): *Method cannot implement interface accessor for type. Use an explicit interface implementation.* +- [**CS0473**](#ambiguous-and-conflicting-implementations): *Explicit interface implementation 'method name' matches more than one interface member. Which interface member is actually chosen is implementation-dependent. Consider using a non-explicit implementation instead.* +- [**CS0531**](#interface-declaration-and-syntax): *Interface members cannot have a definition.* +- [**CS0535**](#missing-or-incomplete-implementations): *Member does not implement interface member.* +- [**CS0538**](#interface-declaration-and-syntax): *Member in explicit interface declaration is not an interface.* +- [**CS0539**](#member-matching-and-resolution): *Member in explicit interface declaration is not found among members of the interface that can be implemented*. +- [**CS0540**](#member-matching-and-resolution): *Containing type does not implement interface member.* +- [**CS0541**](#interface-declaration-and-syntax): *Explicit interface declaration can only be declared in a class, record, struct or interface.* +- [**CS0550**](#missing-or-incomplete-implementations): *Member adds an accessor not found in interface member.* +- [**CS0551**](#missing-or-incomplete-implementations): *Explicit interface implementation is missing an accessor.* +- [**CS0630**](#special-implementation-restrictions): *Member cannot implement interface member because it has an __arglist parameter.* +- [**CS0686**](#accessor-implementation-and-conflicts): *Accessor cannot implement interface member. Use an explicit interface implementation.* +- [**CS0736**](#method-visibility-and-modifiers): *Member does not implement instance interface member. It cannot implement the interface member because it is static.* +- [**CS0737**](#method-visibility-and-modifiers): *Member does not implement interface member. It cannot implement an interface member because it is not public.* +- [**CS0738**](#return-types-and-signatures): *Member does not implement interface member. It cannot because it does not have the matching return type.* +- [**CS8705**](#ambiguous-and-conflicting-implementations): *Interface member does not have a most specific implementation. Neither member is most specific.* +- [**CS8854**](#return-types-and-signatures): *Member does not implement interface member.* +- [**CS9333**](#return-types-and-signatures): *Parameter type must match implemented member.* +- [**CS9334**](#return-types-and-signatures): *Return type must match implemented member.* + +## Interface declaration and syntax + +The following errors relate to proper syntax and structure when declaring explicit interface implementations: + +- **CS0071**: *An explicit interface implementation of an event must use event accessor syntax.* +- **CS0106**: *The modifier is not valid for this item.* +- **CS0531**: *Interface members cannot have a definition.* +- **CS0538**: *Member in explicit interface declaration is not an interface.* +- **CS0541**: *Explicit interface declaration can only be declared in a class, record, struct or interface.* + +You can correct these errors using the following techniques: + +- You must manually provide `add` and `remove` event accessors when explicitly implementing an interface event (**CS0071**). The compiler doesn't automatically generate these accessors for explicit interface implementations, so you must define them explicitly to specify how the event is stored and managed. +- Remove the `public` modifier from explicit interface implementations (**CS0106**). Explicit interface implementations are implicitly public when accessed through the interface type, making the `public` keyword redundant and not allowed in this context. +- Remove the `abstract` modifier from explicit interface implementations (**CS0106**). Explicit interface implementations provide the actual implementation and can't be marked as abstract because they can't be overridden in derived classes. +- Remove the method body from interface member declarations, or move the implementation to a class or struct that implements the interface (**CS0531**). Before C# 8.0, interface members can't contain implementations; starting with C# 8.0, you can provide [default interface methods](../keywords/interface.md#default-interface-members) using specific syntax. +- Verify that the type specified in the explicit interface declaration is an actual interface type (**CS0538**). Only interface types can be used in explicit interface implementation syntax; attempting to use a class or other non-interface type violates the explicit implementation rules. +- Move explicit interface declarations into a class or struct that declares the interface in its base list (**CS0541**). Explicit interface implementations must appear within the body of a class or struct type and can't be declared at the namespace level or in other contexts. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md), and [How to implement interface events](../../programming-guide/events/how-to-implement-interface-events.md). + +## Return types and signatures + +The following errors occur when the implementing method's signature doesn't match the interface member declaration: + +- **CS0738**: *Member does not implement interface member. It cannot because it does not have the matching return type.* +- **CS8854**: *Member does not implement interface member.* +- **CS9333**: *Parameter type must match implemented member.* +- **CS9334**: *Return type must match implemented member.* + +You can correct these errors using the following techniques: + +- Change the return type of the implementing method to exactly match the return type declared in the interface member (**CS0738**, **CS9334**). The signature of the implementation must match the interface declaration precisely because the method signature is part of the contract that determines which interface member is being implemented. +- Ensure that parameter types in the implementing method exactly match the parameter types declared in the interface member (**CS9333**). Each parameter must have the identical type in the same position as specified in the interface declaration, as parameter types are fundamental components of the method signature that the compiler uses to match implementations to interface members. +- Add an `init` accessor to the implementing property when the interface property declares an `init` setter (**CS8854**). The `init` keyword allows property initialization during object construction while preventing modification afterward, and the implementing property must provide this same initialization-only behavior to satisfy the interface contract. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Init-only setters](../keywords/init.md). + +## Missing or incomplete implementations + +The following errors occur when a class fails to fully implement an interface or implements members that don't match the interface contract: + +- **CS0535**: *Member does not implement interface member.* +- **CS0550**: *Member adds an accessor not found in interface member.* +- **CS0551**: *Explicit interface implementation is missing an accessor.* + +You can correct these errors using the following techniques: + +- Provide an implementation for every member declared in the interface, or declare the type as `abstract` (**CS0535**). Each member must be implemented to satisfy the interface requirements. +- Remove any accessors from the implementing property that aren't declared in the interface property (**CS0550**). The implementing property can only include the accessors explicitly declared in the interface definition, ensuring that the implementation doesn't add functionality beyond what the interface contract specifies. +- Add all required accessors to the explicit interface implementation to match the interface declaration (**CS0551**). Each accessor declared in the interface must have a corresponding accessor in the implementation with matching signatures, as the implementation must fulfill the complete accessor contract defined by the interface. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Properties](../../programming-guide/classes-and-structs/properties.md). + +## Member matching and resolution + +The following errors occur when attempting to implement interface members that don't exist in the interface or when the containing type doesn't declare the interface: + +- **CS0539**: *Member in explicit interface declaration is not found among members of the interface that can be implemented*. +- **CS0540**: *Containing type does not implement interface member.* + +You can correct these errors using the following techniques: + +- Verify that the member name and signature in the explicit interface implementation exactly match a member declared in the interface, or remove the incorrect implementation (**CS0539**). The member you're attempting to implement must actually exist in the interface definition with matching name, return type, and parameter types, as explicit interface implementation requires precise correspondence with the interface contract. +- Add the interface to the class's or struct's base list, or remove the explicit interface implementation (**CS0540**). A type can only explicitly implement members of interfaces that it declares in its inheritance list, so the implementing type must establish the interface relationship before it can provide explicit implementations. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md). + +## Generic type constraints + +The following errors occur when implementing generic interface methods with type parameter constraints: + +- **CS0425**: *The constraints for type parameter of method must match the constraints for type parameter of interface method. Consider using an explicit interface implementation instead.* +- **CS0460**: *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly, except for either a 'class', or a 'struct' constraint.* + +You can correct these errors using the following techniques: + +- Ensure the `where` clause in the implementing method is identical to the interface method declaration, or matches the semantic meaning of the constraints (**CS0425**). The type parameter constraints in the implementation must match those defined in the interface or base method. +- Remove explicit constraint declarations from override and explicit interface implementation methods (**CS0460**). The override method inherits its constraints automatically from the base or interface method, so redeclaring them is redundant and not permitted except for specific cases allowed in C# 8 and later. +- Apply the `default` constraint to resolve ambiguities with nullable reference types in override and explicit interface implementations when using C# 9 or later (**CS0460**). This exception to the constraint inheritance rule allows you to explicitly specify the default constraint to disambiguate nullable annotation contexts. +- Explicitly specify `where T : class` or `where T : struct` constraints on override and explicit interface implementation methods when using C# 8 or later to enable nullable reference type annotations (**CS0460**). These specific constraints are permitted to support nullable reference type analysis on type parameters that are constrained to reference or value types. + +For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md), [Interfaces](../../fundamentals/types/interfaces.md), and [Nullable reference types](../../nullable-references.md). + +## Method visibility and modifiers + +The following errors occur when implementing interface methods with incorrect accessibility or modifiers: + +- **CS0736**: *Member does not implement instance interface member. It cannot implement the interface member because it is static.* +- **CS0737**: *Member does not implement interface member. It cannot implement an interface member because it is not public.* + +You can correct these errors using the following techniques: + +- Remove the `static` modifier from the method declaration that implements the interface member (**CS0736**). Before C# 10, interface members are instance members, not static members. +- Add the `public` access modifier to the method that implements the interface member (**CS0737**). All interface members are implicitly `public` because interfaces define a contract for public behavior, so the implementing method must also have public accessibility to be accessible through the interface reference. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md). + +## Accessor implementation and conflicts + +The following errors occur when implementing interface properties or events with accessor methods that have visibility issues or naming conflicts: + +- **CS0277**: *Member does not implement interface member because it is not public.* +- **CS0470**: *Method cannot implement interface accessor for type. Use an explicit interface implementation.* +- **CS0686**: *Accessor cannot implement interface member. Use an explicit interface implementation.* + +You can correct these errors using the following techniques: + +- Remove any access modifiers from property accessors that restrict visibility to less visible than `public`, or add the `public` modifier if it's missing (**CS0277**). All interface members are implicitly `public`, so the implementing accessor must also have public accessibility to satisfy the interface contract and be accessible through the interface type. +- Replace methods with accessor-like names (such as `get_PropertyName`) with proper property syntax using explicit interface implementation (**CS0470**). The compiler generates accessor methods internally for properties, and attempting to manually create methods with these reserved names conflicts with the property implementation mechanism. +- Use explicit interface implementation syntax to resolve naming conflicts when the interface contains method names that match the auto-generated accessor methods (**CS0686**). The compiler automatically generates methods like `get_Property` and `set_Property` for properties, and `add_Event` and `remove_Event` for events, so if an interface declares methods with these exact names, explicit implementation is required to disambiguate between the interface method and the compiler-generated accessor. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Events](../../programming-guide/events/index.md). + +## Ambiguous and conflicting implementations + +The following errors occur when the compiler can't determine which interface implementation to use: + +- **CS0473**: *Explicit interface implementation 'method name' matches more than one interface member. Which interface member is actually chosen is implementation-dependent. Consider using a non-explicit implementation instead.* +- **CS8705**: *Interface member 'member' does not have a most specific implementation. Neither is most specific.* + +You can correct these errors using the following techniques: + +- Eliminate the explicit interface implementation and instead use a single implicit public implementation for both interface methods (**CS0473**). When a generic method acquires the same signature as a non-generic method (such as when implementing `ITest` where both `TestMethod(int)` and `TestMethod(T)` become identical), the common language infrastructure metadata system can't unambiguously determine which interface member binds to which implementation slot, so using implicit implementation allows the single method to satisfy both interface requirements. +- Provide an explicit implementation in the implementing class or struct that resolves the ambiguity between multiple default implementations (**CS8705**). This error typically occurs with diamond inheritance patterns where a class implements multiple interfaces that each provide default implementations for the same member. The compiler needs you to explicitly specify which implementation to use, or provide your own implementation. +- Restructure the interface hierarchy to avoid diamond inheritance conflicts where multiple interfaces provide default implementations for the same member (**CS8705**). By redesigning the interface relationships or consolidating the default implementations into a single interface, you can eliminate the ambiguity that prevents the compiler from determining the most specific implementation. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Default Interface Methods](../keywords/interface.md#default-interface-members). + +## Special implementation restrictions + +The following error occurs when using special parameter types that aren't compatible with interface implementation: + +- **CS0630**: *Member cannot implement interface member because it has an __arglist parameter.* + +You can correct this error using the following techniques: + +- Remove the `__arglist` parameter from the implementing method (**CS0630**). The `__arglist` keyword allows methods to accept variable numbers of arguments in an unmanaged way, but this feature is incompatible with interface implementation because interface contracts require predictable, type-safe signatures that can be verified at compile time. +- Replace the `__arglist` parameter with a `params` array parameter for variable-length argument lists (**CS0630**). Unlike `__arglist`, the `params` keyword provides a type-safe mechanism for accepting variable numbers of arguments that is fully compatible with interface implementation and maintains the compile-time type safety that interfaces require. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [params keyword](../keywords/method-parameters.md#params-modifier). diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index ac5e564fed106..c183361da12fd 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -509,12 +509,26 @@ 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: > async, await, CS1983, CS1985, CS1986, CS1989, CS1991, CS1992, CS1994, CS1995, CS1996, CS1997, CS1998, CS4008, CS4009, CS4014, CS4032, CS4033, CS8892, CS9123, CS9330 + - name: Interface implementation + href: ./compiler-messages/interface-implementation-errors.md + displayName: > + interface, + CS0071, CS0106, CS0277, CS0425, CS0460, CS0470, CS0473, CS0531, CS0535, CS0538, CS0539, CS0540, + CS0541, CS0550, CS0551, CS0630, CS0686, CS0736, CS0737, CS0738, CS8705, CS8707, CS8711, CS8854, + CS9333, CS9334 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > @@ -752,8 +766,6 @@ items: href: ../misc/cs0069.md - name: CS0070 href: ../misc/cs0070.md - - name: CS0071 - href: ./compiler-messages/cs0071.md - name: CS0072 href: ../misc/cs0072.md - name: CS0073 @@ -768,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 @@ -784,8 +792,6 @@ items: href: ./compiler-messages/cs0103.md - name: CS0104 href: ../misc/cs0104.md - - name: CS0106 - href: ./compiler-messages/cs0106.md - name: CS0107 href: ../misc/cs0107.md - name: CS0110 @@ -956,40 +962,14 @@ items: href: ../misc/cs0275.md - name: CS0276 href: ../misc/cs0276.md - - name: CS0277 - href: ../misc/cs0277.md - name: CS0281 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 @@ -1002,20 +982,12 @@ 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 href: ../misc/cs0423.md - name: CS0424 href: ../misc/cs0424.md - - name: CS0425 - href: ../misc/cs0425.md - name: CS0426 href: ../misc/cs0426.md - name: CS0428 @@ -1056,8 +1028,6 @@ items: href: ../misc/cs0455.md - name: CS0456 href: ../misc/cs0456.md - - name: CS0460 - href: ../misc/cs0460.md - name: CS0462 href: ../misc/cs0462.md - name: CS0463 @@ -1068,8 +1038,6 @@ items: href: ../misc/cs0470.md - name: CS0471 href: ../misc/cs0471.md - - name: CS0473 - href: ../misc/cs0473.md - name: CS0500 href: ../misc/cs0500.md - name: CS0502 @@ -1106,22 +1074,16 @@ items: href: ../misc/cs0528.md - name: CS0529 href: ../misc/cs0529.md - - name: CS0531 - href: ../misc/cs0531.md - name: CS0533 href: ../misc/cs0533.md - name: CS0534 href: ../misc/cs0534.md - - name: CS0535 - href: ../misc/cs0535.md - name: CS0537 href: ../misc/cs0537.md - name: CS0538 href: ../misc/cs0538.md - name: CS0539 href: ../misc/cs0539.md - - name: CS0540 - href: ../misc/cs0540.md - name: CS0541 href: ../misc/cs0541.md - name: CS0542 @@ -1140,8 +1102,6 @@ items: href: ../misc/cs0548.md - name: CS0549 href: ../misc/cs0549.md - - name: CS0550 - href: ../misc/cs0550.md - name: CS0551 href: ../misc/cs0551.md - name: CS0569 @@ -1232,8 +1192,6 @@ items: href: ../misc/cs0682.md - name: CS0683 href: ../misc/cs0683.md - - name: CS0686 - href: ./compiler-messages/cs0686.md - name: CS0689 href: ../misc/cs0689.md - name: CS0690 @@ -1302,12 +1260,6 @@ items: href: ../misc/cs0734.md - name: CS0735 href: ../misc/cs0735.md - - name: CS0736 - href: ../misc/cs0736.md - - name: CS0737 - href: ../misc/cs0737.md - - name: CS0738 - href: ../misc/cs0738.md - name: CS0739 href: ../misc/cs0739.md - name: CS0742 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/cs0277.md b/docs/csharp/misc/cs0277.md deleted file mode 100644 index 9c003bec4f271..0000000000000 --- a/docs/csharp/misc/cs0277.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -description: "Compiler Error CS0277" -title: "Compiler Error CS0277" -ms.date: 07/20/2015 -f1_keywords: - - "CS0277" -helpviewer_keywords: - - "CS0277" -ms.assetid: 8abec3eb-4d4c-4aab-87cc-d0444ab23535 ---- -# Compiler Error CS0277 - -'class' does not implement interface member 'accessor'. 'class accessor' is not public - - This error occurs when you try to implement a property of an interface, but the implementation of the property accessor in the class is not public. Methods that implement interface members need to have public accessibility. To resolve, remove the access modifier on the property accessor. - -## Example - - The following example generates CS0277: - -```csharp -// CS0277.cs -public interface MyInterface -{ - int Property - { - get; - set; - } -} - -public class MyClass : MyInterface // CS0277 -{ - public int Property - { - get { return 0; } - // Try this instead: - //set { } - protected set { } - } -} -``` 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/cs0425.md b/docs/csharp/misc/cs0425.md deleted file mode 100644 index c61a74ff81f69..0000000000000 --- a/docs/csharp/misc/cs0425.md +++ /dev/null @@ -1,78 +0,0 @@ ---- -description: "Compiler Error CS0425" -title: "Compiler Error CS0425" -ms.date: 07/20/2015 -f1_keywords: - - "CS0425" -helpviewer_keywords: - - "CS0425" -ms.assetid: cec0391c-a641-43bc-8557-92b23f6ca685 ---- -# Compiler Error CS0425 - -The constraints for type parameter 'type parameter' of method 'method' must match the constraints for type parameter 'type parameter' of interface method 'method'. Consider using an explicit interface implementation instead. - - This error occurs if a virtual generic method is overridden in a derived class and the constraints on the method in the derived class do not match the constraints on the method in the base class. To avoid this error, make sure the `where` clause is identical in both declarations, or implement the interface explicitly. - -## Example 1 - - The following example generates CS0425: - -```csharp -// CS0425.cs - -class C1 -{ -} - -class C2 -{ -} - -interface IBase -{ - void F(ItemType item) where ItemType : C1; -} - -class Derived : IBase -{ - public void F(ItemType item) where ItemType : C2 // CS0425 - { - } -} - -class CMain -{ - public static void Main() - { - } -} -``` - -## Example 2 - - The constraints do not have to be a literal match, as long as the set of constraints has the same meaning. For example, the following is okay: - -```csharp -// CS0425b.cs - -interface J -{ -} - -interface I -{ - void F(S s, T t) where T: J, J; -} - -class C : I -{ - public void F(int s, X x) where X : J - { - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0460.md b/docs/csharp/misc/cs0460.md deleted file mode 100644 index 93633a953186f..0000000000000 --- a/docs/csharp/misc/cs0460.md +++ /dev/null @@ -1,57 +0,0 @@ ---- -description: "Compiler Error CS0460" -title: "Compiler Error CS0460" -ms.date: 06/03/2025 -ai-usage: ai-generated -f1_keywords: - - "CS0460" -helpviewer_keywords: - - "CS0460" -ms.assetid: 98d39ded-d3f9-4520-b912-892e574c056b ---- -# Compiler Error CS0460 - -Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly - - When a generic method that is part of a derived class overrides a method in the base class, you can't specify constraints on the overridden method. The override method in the derived class inherits its constraints from the method in the base class. - -However, there are exceptions to this rule: - -- Starting with C# 9, you can apply the `default` constraint to `override` and explicit interface implementation methods to resolve ambiguities with nullable reference types. -- Starting with C# 8, you can explicitly specify `where T : class` and `where T : struct` constraints on `override` and explicit interface implementation methods to allow annotations for type parameters constrained to reference types. - -## Example - - The following sample generates CS0460 when attempting to redeclare inherited constraints. - -```csharp -// CS0460.cs -// compile with: /target:library -class BaseClass -{ - BaseClass() { } -} - -interface I -{ - void F1() where T : BaseClass; - void F2() where T : struct; - void F3(); - void F4() where T : struct; -} - -class ExpImpl : I -{ - // CS0460 - cannot redeclare inherited constraint. - void I.F1() where T : BaseClass { } - - // Allowed - explicit constraint for struct. - void I.F2() where T : struct { } - - // Valid since C# 8 - explicit class constraint for nullable annotations. - void I.F4() where T : struct { } - - // Valid since C# 9 - default constraint to resolve ambiguities. - void I.F3() where T : default { } -} -``` diff --git a/docs/csharp/misc/cs0470.md b/docs/csharp/misc/cs0470.md deleted file mode 100644 index e26f2fa76725b..0000000000000 --- a/docs/csharp/misc/cs0470.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS0470" -title: "Compiler Error CS0470" -ms.date: 07/20/2015 -f1_keywords: - - "CS0470" -helpviewer_keywords: - - "CS0470" -ms.assetid: b5a8e820-aa5c-4f69-b5c6-01c6a6bb82d9 ---- -# Compiler Error CS0470 - -Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation. - - This error is generated when an accessor is trying to implement an interface. Explicit interface implementation must be used. - -## Example - - The following sample generates CS0470. - -```csharp -// CS0470.cs -// compile with: /target:library - -interface I -{ - int P { get; } -} - -class MyClass : I -{ - public int get_P() { return 0; } // CS0470 - public int P2 { get { return 0;} } // OK -} -``` diff --git a/docs/csharp/misc/cs0473.md b/docs/csharp/misc/cs0473.md deleted file mode 100644 index 154f06fe2c8af..0000000000000 --- a/docs/csharp/misc/cs0473.md +++ /dev/null @@ -1,65 +0,0 @@ ---- -description: "Compiler Error CS0473" -title: "Compiler Error CS0473" -ms.date: 08/14/2018 -f1_keywords: - - "CS0473" -helpviewer_keywords: - - "CS0473" -ms.assetid: 58eb141e-7da0-41c8-b868-7cd2a15f07f9 ---- -# Compiler Error CS0473 - -Explicit interface implementation 'method name' matches more than one interface member. Which interface member is actually chosen is implementation-dependent. Consider using a non-explicit implementation instead. - -In some cases a generic method might acquire the same signature as a non-generic method. The problem is that there is no way in the common language infrastructure (CLI) metadata system to unambiguously state which method binds to which slot. It is up to the CLI to make that determination. - -## To correct this error - -To correct the error, eliminate the explicit implementation and implement both of the interface methods in the implicit implementation `public int TestMethod(int)`. - -## Example - -The following code generates CS0473: - -```csharp -public interface ITest -{ - int TestMethod(int i); - int TestMethod(T i); -} - -public class ImplementingClass : ITest -{ - int ITest.TestMethod(int i) // CS0473 - { - return i + 1; - } - - public int TestMethod(int i) - { - return i - 1; - } -} - -class T -{ - static int Main() - { - ImplementingClass a = new ImplementingClass(); - if (a.TestMethod(0) != -1) - return -1; - - ITest i_a = a; - System.Console.WriteLine(i_a.TestMethod(0).ToString()); - if (i_a.TestMethod(0) != 1) - return -1; - - return 0; - } -} -``` - -## See also - -- [Odious ambiguous overloads, part two](/archive/blogs/ericlippert/odious-ambiguous-overloads-part-two) diff --git a/docs/csharp/misc/cs0531.md b/docs/csharp/misc/cs0531.md deleted file mode 100644 index ec8b67d32b02e..0000000000000 --- a/docs/csharp/misc/cs0531.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0531" -title: "Compiler Error CS0531" -ms.date: 07/20/2015 -f1_keywords: - - "CS0531" -helpviewer_keywords: - - "CS0531" -ms.assetid: 54c2a98b-84e3-481a-a934-7cd6dffa7677 ---- -# Compiler Error CS0531 - -'member' : interface members cannot have a definition - - Methods that are declared in an [interface](../language-reference/keywords/interface.md) must be implemented in a class that inherits from it and not in the interface itself. - - The following sample generates CS0531: - -```csharp -// CS0531.cs -namespace x -{ - public interface clx - { - int xclx() // CS0531, cannot define xclx - // Try the following declaration instead: - // int xclx(); - { - return 0; - } - } - - public class cly - { - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0535.md b/docs/csharp/misc/cs0535.md deleted file mode 100644 index 970a934aae631..0000000000000 --- a/docs/csharp/misc/cs0535.md +++ /dev/null @@ -1,55 +0,0 @@ ---- -description: "Compiler Error CS0535" -title: "Compiler Error CS0535" -ms.date: 07/20/2015 -f1_keywords: - - "CS0535" -helpviewer_keywords: - - "CS0535" -ms.assetid: 282ed5d6-acb7-445b-999f-27a973ccc0b5 ---- -# Compiler Error CS0535 - -'class' does not implement interface member 'member' - - A [class](../language-reference/keywords/class.md) derived from an [interface](../language-reference/keywords/interface.md), but the class did not implement one or more of the interface's members. A class must implement all members of interfaces from which it derives or else be declared `abstract`. - -## Example 1 - - The following sample generates CS0535. - -```csharp -// CS0535.cs -public interface A -{ - void F(); -} - -public class B : A {} // CS0535 A::F is not implemented - -// OK -public class C : A { - public void F() {} - public static void Main() {} -} -``` - -## Example 2 - - The following sample generates CS0535. - -```csharp -// CS0535_b.cs -using System; -class C : IDisposable {} // CS0535 - -// OK -class D : IDisposable { - void IDisposable.Dispose() {} - public void Dispose() {} - - static void Main() { - using (D d = new D()) {} - } -} -``` diff --git a/docs/csharp/misc/cs0538.md b/docs/csharp/misc/cs0538.md deleted file mode 100644 index 4a48a08193638..0000000000000 --- a/docs/csharp/misc/cs0538.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0538" -title: "Compiler Error CS0538" -ms.date: 07/20/2015 -f1_keywords: - - "CS0538" -helpviewer_keywords: - - "CS0538" -ms.assetid: 46ac205e-16b0-4637-bd0f-9a755ac19f18 ---- -# Compiler Error CS0538 - -'name' in explicit interface declaration is not an interface - - An attempt was made to explicitly declare an [interface](../language-reference/keywords/interface.md), but an interface was not specified. - - The following sample generates CS0538: - -```csharp -// CS0538.cs -interface MyIFace -{ - void F(); -} - -public class MyClass -{ - public void G() - { - } -} - -class C: MyIFace -{ - void MyIFace.F() - { - } - - void MyClass.G() // CS0538, MyClass not an interface - { - } -} -``` diff --git a/docs/csharp/misc/cs0539.md b/docs/csharp/misc/cs0539.md deleted file mode 100644 index 3d96081280b1d..0000000000000 --- a/docs/csharp/misc/cs0539.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0539" -title: "Compiler Error CS0539" -ms.date: 07/20/2015 -f1_keywords: - - "CS0539" -helpviewer_keywords: - - "CS0539" -ms.assetid: 41b8975c-abd1-4a36-98a4-8efa5fb0502a ---- -# Compiler Error CS0539 - -'member' in explicit interface declaration is not a member of interface - - An attempt was made to explicitly declare an [interface](../language-reference/keywords/interface.md) member that does not exist. You should either delete the declaration or change it so that it refers to a valid interface member. - - The following sample generates CS0539: - -```csharp -// CS0539.cs -namespace x -{ - interface I - { - void m(); - } - - public class clx : I - { - void I.x() // CS0539 - { - } - - public static int Main() - { - return 0; - } - } -} -``` diff --git a/docs/csharp/misc/cs0540.md b/docs/csharp/misc/cs0540.md deleted file mode 100644 index 6f50275f81eb0..0000000000000 --- a/docs/csharp/misc/cs0540.md +++ /dev/null @@ -1,60 +0,0 @@ ---- -description: "Compiler Error CS0540" -title: "Compiler Error CS0540" -ms.date: 07/20/2015 -f1_keywords: - - "CS0540" -helpviewer_keywords: - - "CS0540" -ms.assetid: 2da2cd4a-0ff1-45ea-bb72-ea078bc95dea ---- -# Compiler Error CS0540 - -'interface member' : containing type does not implement interface 'interface' - - You attempted to implement an interface member in a [class](../language-reference/keywords/class.md) that does not derive from the [interface](../language-reference/keywords/interface.md). You should either delete the implementation of the interface member or add the interface to the base-class list of the class. - -## Example 1 - - The following sample generates CS0540. - -```csharp -// CS0540.cs -interface I -{ - void m(); -} - -public class Clx -{ - void I.m() {} // CS0540 -} - -// OK -public class Cly : I -{ - void I.m() {} - public static void Main() {} -} -``` - -## Example 2 - - The following sample generates CS0540. - -```csharp -// CS0540_b.cs -using System; -class C { - void IDisposable.Dispose() {} // CS0540 -} - -class D : IDisposable { - void IDisposable.Dispose() {} - public void Dispose() {} - - static void Main() { - using (D d = new D()) {} - } -} -``` diff --git a/docs/csharp/misc/cs0541.md b/docs/csharp/misc/cs0541.md deleted file mode 100644 index 54616a962bee9..0000000000000 --- a/docs/csharp/misc/cs0541.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS0541" -title: "Compiler Error CS0541" -ms.date: 07/20/2015 -f1_keywords: - - "CS0541" -helpviewer_keywords: - - "CS0541" -ms.assetid: ed812c07-24f7-43c6-9a44-553f27f6249d ---- -# Compiler Error CS0541 - -'declaration' : explicit interface declaration can only be declared in a class or struct - - An explicit [interface](../language-reference/keywords/interface.md) declaration was found outside a [class](../language-reference/keywords/class.md) or [struct](../language-reference/builtin-types/struct.md). - - The following sample generates CS0541: - -```csharp -// CS0541.cs -namespace x -{ - interface IFace - { - void F(); - } - - interface IFace2 : IFace - { - void IFace.F(); // CS0541 - } -} -``` diff --git a/docs/csharp/misc/cs0550.md b/docs/csharp/misc/cs0550.md deleted file mode 100644 index 64daba3a2be35..0000000000000 --- a/docs/csharp/misc/cs0550.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -description: "Compiler Error CS0550" -title: "Compiler Error CS0550" -ms.date: 07/20/2015 -f1_keywords: - - "CS0550" -helpviewer_keywords: - - "CS0550" -ms.assetid: 57278c17-443c-40f2-9ebd-853558743564 ---- -# Compiler Error CS0550 - -'accessor' adds an accessor not found in interface member 'property' - - The implementation of a property in a derived class contains an accessor that was not specified in the base interface. - - For more information, see [Using Properties](../programming-guide/classes-and-structs/using-properties.md). - -## Example - - The following sample generates CS0550. - -```csharp -// CS0550.cs -namespace x -{ - interface ii - { - int i - { - get; - // add the following accessor to resolve this CS0550 - // set; - } - } - - public class a : ii - { - int ii.i - { - get - { - return 0; - } - set {} // CS0550 no set in interface - } - - public static void Main() {} - } -} -``` diff --git a/docs/csharp/misc/cs0551.md b/docs/csharp/misc/cs0551.md deleted file mode 100644 index fbfc60a40afcb..0000000000000 --- a/docs/csharp/misc/cs0551.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS0551" -title: "Compiler Error CS0551" -ms.date: 07/20/2015 -f1_keywords: - - "CS0551" -helpviewer_keywords: - - "CS0551" -ms.assetid: fb456ecf-dff3-4e39-b9b3-de23d81dadea ---- -# Compiler Error CS0551 - -Explicit interface implementation 'implementation' is missing accessor 'accessor' - - A class that explicitly implements an interface's property must implement all the accessors that the interface defines. - - For more information, see [Using Properties](../programming-guide/classes-and-structs/using-properties.md). - -## Example - - The following sample generates CS0551. - -```csharp -// CS0551.cs -// compile with: /target:library -interface ii -{ - int i - { - get; - set; - } -} - -public class a : ii -{ - int ii.i { set {} } // CS0551 - - // OK - int ii.i - { - set {} - get { return 0; } - } -} -``` 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/cs0736.md b/docs/csharp/misc/cs0736.md deleted file mode 100644 index 60b8c2adf84cc..0000000000000 --- a/docs/csharp/misc/cs0736.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -description: "Compiler Error CS0736" -title: "Compiler Error CS0736" -ms.date: 07/20/2015 -f1_keywords: - - "CS0736" -helpviewer_keywords: - - "CS0736" -ms.assetid: 06b14feb-81d5-495f-ab2d-6dc3f5e7216f ---- -# Compiler Error CS0736 - -'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static. - - This error is generated when a static method is either implicitly or explicitly declared as an implementation of an interface member. - -## To correct this error - -- Remove the [static](../language-reference/keywords/static.md) modifier from the method declaration. - -- Change the name of the interface method. - -- Redefine the containing type so that it does not inherit from the interface. - -## Example - - The following code generates CS0736 because `Program.testMethod` is declared as static: - -```csharp -// cs0736.cs -namespace CS0736 -{ - - interface ITest - { - int testMethod(int x); - } - - class Program : ITest // CS0736 - { - public static int testMethod(int x) { return 0; } - // Try the following line instead. - // public int testMethod(int x) { return 0; } - public static void Main() { } - } -} -``` - -## See also - -- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/cs0737.md b/docs/csharp/misc/cs0737.md deleted file mode 100644 index e2354d4ed4cab..0000000000000 --- a/docs/csharp/misc/cs0737.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -description: "Compiler Error CS0737" -title: "Compiler Error CS0737" -ms.date: 07/20/2015 -f1_keywords: - - "CS0737" -helpviewer_keywords: - - "CS0737" -ms.assetid: d2247770-5546-46f2-a01d-8e2ebfcbb859 ---- -# Compiler Error CS0737 - -'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public. - - A method that implements an interface member must have public accessibility. All interface members are `public`. - -## To correct this error - -1. Add the [public](../language-reference/keywords/public.md) access modifier to the method. - -## Example - - The following code generates CS0737: - -```csharp -// cs0737.cs -interface ITest -{ - // Default access of private with no modifier. - int Return42(); - // Try the following line instead. - // public int Return42(); -} - -struct Struct1 : ITest // CS0737 -{ - int Return42() { return (42); } -} - -public class Test -{ - public static int Main(string[] args) - { - Struct1 s1 = new Struct1(); - - return (1); - } - -} -``` - -## See also - -- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/cs0738.md b/docs/csharp/misc/cs0738.md deleted file mode 100644 index 2083f426ac71b..0000000000000 --- a/docs/csharp/misc/cs0738.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -description: "Compiler Error CS0738" -title: "Compiler Error CS0738" -ms.date: 07/20/2015 -f1_keywords: - - "CS0738" -helpviewer_keywords: - - "CS0738" -ms.assetid: 01ce94ee-2435-4326-befc-2b020c441a4f ---- -# Compiler Error CS0738 - -'type name' does not implement interface member 'member name'. 'method name' cannot implement 'interface member' because it does not have the matching return type of ' type name'. - - The return value of an implementing method in a class must match the return value of the interface member that it implements. - -## To correct this error - -1. Change the return type of the method to match that of the interface member. - -## Example - - The following code generates CS0738 because the class method returns `void` and the interface member of the same name returns `int`: - -```csharp -using System; - -interface ITest -{ - int TestMethod(); -} -public class Test: ITest -{ - public void TestMethod() { } // CS0738 - // Try the following line instead. - // public int TestMethod(); -} -``` - -## See also - -- [Interfaces](../fundamentals/types/interfaces.md) 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 7a25597850dd4..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,10 +4,8 @@ description: "List of possible resources for compiler errors and warnings that h ms.date: 05/23/2025 f1_keywords: - "CS0190" - - "CS0224" - "CS0257" - "CS0595" - - "CS0630" - "CS0847" - "CS0856" - "CS0857" @@ -339,9 +337,7 @@ f1_keywords: - "CS8700" - "CS8701" - "CS8702" - - "CS8705" - "CS8707" - - "CS8711" - "CS8712" - "CS8715" - "CS8716" @@ -394,7 +390,6 @@ f1_keywords: - "CS8851" - "CS8852" - "CS8853" - - "CS8854" - "CS8855" - "CS8856" - "CS8857" @@ -537,10 +532,6 @@ f1_keywords: - "CS9095" - "CS9096" - "CS9097" -# C# 14 errors begin here - - "CS9333" - - "CS9334" - - "CS9338" helpviewer_keywords: - "errors [C#], additional information" --- diff --git a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md index 138e11207db6d..4621e6c217a2b 100644 --- a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md +++ b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md @@ -28,7 +28,7 @@ Explicit implementation is also used to resolve cases where two interfaces each [!code-csharp[NameCollisions](~/samples/snippets/csharp/interfaces/ExplicitImplementation.cs#NameCollision)] -An explicit interface implementation doesn't have an access modifier since it isn't accessible as a member of the type it's defined in. Instead, it's only accessible when called through an instance of the interface. If you specify an access modifier for an explicit interface implementation, you get compiler error [CS0106](../../language-reference/compiler-messages/cs0106.md). For more information, see [`interface` (C# Reference)](../../language-reference/keywords/interface.md). +An explicit interface implementation doesn't have an access modifier since it isn't accessible as a member of the type it's defined in. Instead, it's only accessible when called through an instance of the interface. If you specify an access modifier for an explicit interface implementation, you get compiler error [CS0106](../../language-reference/compiler-messages/interface-implementation-errors.md). For more information, see [`interface` (C# Reference)](../../language-reference/keywords/interface.md). You can define an implementation for members declared in an interface. If a class inherits a method implementation from an interface, that method is only accessible through a reference of the interface type. The inherited member doesn't appear as part of the public interface. The following sample defines a default implementation for an interface method: diff --git a/docs/whats-new/index.yml b/docs/whats-new/index.yml index e4b1a83bd2db9..5e96a2d85e710 100644 --- a/docs/whats-new/index.yml +++ b/docs/whats-new/index.yml @@ -40,10 +40,10 @@ landingContent: linkLists: - linkListType: whats-new links: - - text: What's new in C# 13 - url: ../csharp/whats-new/csharp-13.md - - text: What's new in F# 9 - url: ../fsharp/whats-new/fsharp-9.md + - text: What's new in C# 14 + url: ../csharp/whats-new/csharp-14.md + - text: What's new in F# 10 + url: ../fsharp/whats-new/fsharp-10.md - text: What's new for Visual Basic url: ../visual-basic/whats-new/index.md - title: Contribute to docs