From 8a8dca10fe6f53866bb1d239275d52f89720ad4a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 10:43:59 -0500 Subject: [PATCH 01/11] Create shell for interface issues Create shell, TOC, and placeholder for error consolidation related to interface implementations. --- .github/prompts/error-consolidation.md | 4 +-- .../explicit-interface-errors.md | 28 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 5 ++++ ...n-t-have-specifics-on-this-csharp-error.md | 2 -- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index c96c2f381d53d..e7fabb653e4d1 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 `explicit-interface-errors.md`. +- For the duration of this chat, all references to "the target theme" refer to errors and warnings related to members that explicitly implement an interface member. The destination file already contains a skeleton for the final output. diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md new file mode 100644 index 0000000000000..a21ce33deacf8 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -0,0 +1,28 @@ +--- +title: Resolve errors and warnings that explicit interface implementation. +description: These compiler errors and warnings indicate errors in declaring methods that explicitly implement an interface member. +f1_keywords: + - "CS9333" + - "CS9334" +helpviewer_keywords: + - "CS9333" + - "CS9334" +ms.date: 11/12/2025 +ai-usage: ai-assisted +--- +# Resolve errors and warnings related to attribute declarations or attribute use in your code + +This article covers the following compiler errors: + + +- [**CS9333**](#incorrect-member-signature): *Parameter type must match implemented member declaration.* +- [**CS9334**](#incorrect-member-signature): *Return type must match implemented member declaration.* + +## Incorrect member signature + +The following errors occur when the explicit member implementation doesn't match the declaration + +- **CS9333**: *Parameter type must match implemented member declaration.* +- **CS9334**: *Return type must match implemented member declaration.* diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index ac5e564fed106..7d9425fe62d03 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -515,6 +515,11 @@ items: async, await, CS1983, CS1985, CS1986, CS1989, CS1991, CS1992, CS1994, CS1995, CS1996, CS1997, CS1998, CS4008, CS4009, CS4014, CS4032, CS4033, CS8892, CS9123, CS9330 + - name: Explicit interface implementation + href: ./compiler-messages/explicit-interface-errors.md + displayName: > + interface, + CS9333, CS9334 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > 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..3aa7c8c58f535 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 @@ -538,8 +538,6 @@ f1_keywords: - "CS9096" - "CS9097" # C# 14 errors begin here - - "CS9333" - - "CS9334" - "CS9338" helpviewer_keywords: - "errors [C#], additional information" From a35e36b2835af5b992c59200e88f567952798532 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 12:44:34 -0500 Subject: [PATCH 02/11] Add explicit interface diagnostics --- .openpublishing.redirection.csharp.json | 48 ++ .../compiler-messages/cs0071.md | 52 -- .../compiler-messages/cs0106.md | 70 --- .../compiler-messages/cs0686.md | 76 --- .../explicit-interface-errors.md | 518 ++++++++++++++++++ docs/csharp/language-reference/toc.yml | 18 +- docs/csharp/misc/cs0425.md | 78 --- docs/csharp/misc/cs0460.md | 57 -- docs/csharp/misc/cs0470.md | 35 -- docs/csharp/misc/cs0473.md | 65 --- docs/csharp/misc/cs0538.md | 43 -- docs/csharp/misc/cs0539.md | 40 -- docs/csharp/misc/cs0541.md | 33 -- docs/csharp/misc/cs0551.md | 46 -- docs/csharp/misc/cs0736.md | 51 -- 15 files changed, 568 insertions(+), 662 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0071.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0106.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0686.md delete mode 100644 docs/csharp/misc/cs0425.md delete mode 100644 docs/csharp/misc/cs0460.md delete mode 100644 docs/csharp/misc/cs0470.md delete mode 100644 docs/csharp/misc/cs0473.md delete mode 100644 docs/csharp/misc/cs0538.md delete mode 100644 docs/csharp/misc/cs0539.md delete mode 100644 docs/csharp/misc/cs0541.md delete mode 100644 docs/csharp/misc/cs0551.md delete mode 100644 docs/csharp/misc/cs0736.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index a8867a28f0d8f..6c2db7dda10a9 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -315,6 +315,14 @@ "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/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0106.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-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 +363,14 @@ "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/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0736.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-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 +1849,26 @@ "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/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0460.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-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/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0473.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0501.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-declarations" @@ -1865,6 +1897,22 @@ "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/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0539.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0541.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0551.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-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/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/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/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md index a21ce33deacf8..69c2e515f5bb5 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -2,9 +2,33 @@ title: Resolve errors and warnings that explicit interface implementation. description: These compiler errors and warnings indicate errors in declaring methods that explicitly implement an interface member. f1_keywords: + - "CS0071" + - "CS0106" + - "CS0425" + - "CS0460" + - "CS0470" + - "CS0473" + - "CS0538" + - "CS0539" + - "CS0541" + - "CS0551" + - "CS0686" + - "CS0736" - "CS9333" - "CS9334" helpviewer_keywords: + - "CS0071" + - "CS0106" + - "CS0425" + - "CS0460" + - "CS0470" + - "CS0473" + - "CS0538" + - "CS0539" + - "CS0541" + - "CS0551" + - "CS0686" + - "CS0736" - "CS9333" - "CS9334" ms.date: 11/12/2025 @@ -17,9 +41,503 @@ This article covers the following compiler errors: +- [**CS0071**](#event-accessor-syntax): *An explicit interface implementation of an event must use event accessor syntax* +- [**CS0106**](#invalid-modifiers): *The modifier 'modifier' is not valid for this item* +- [**CS0425**](#constraint-mismatch): *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.* +- [**CS0460**](#inherited-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* +- [**CS0470**](#accessor-implementation): *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* +- [**CS0473**](#ambiguous-match): *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.* +- [**CS0538**](#not-an-interface): *'name' in explicit interface declaration is not an interface* +- [**CS0539**](#member-not-in-interface): *'member' in explicit interface declaration is not a member of interface* +- [**CS0541**](#wrong-location): *'declaration' : explicit interface declaration can only be declared in a class or struct* +- [**CS0551**](#missing-accessor): *Explicit interface implementation 'implementation' is missing accessor 'accessor'* +- [**CS0686**](#accessor-name-conflict): *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* +- [**CS0736**](#static-implementation): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* - [**CS9333**](#incorrect-member-signature): *Parameter type must match implemented member declaration.* - [**CS9334**](#incorrect-member-signature): *Return type must match implemented member declaration.* +## Event accessor syntax + +- **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). + +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() { } +} +``` + +## Invalid modifiers + +- **CS0106**: *The modifier 'modifier' is not valid for this item* + +When declaring explicit interface implementations, certain modifiers are not allowed: + +- 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. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md). + +The following sample generates CS0106: + +```csharp +// CS0106.cs +namespace MyNamespace +{ + interface I + { + void M1(); + void M2(); + } + + public class MyClass : I + { + public void I.M1() {} // CS0106 + abstract void I.M2() {} // CS0106 + + public static void Main() {} + } +} +``` + +## Constraint mismatch + +- **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. + +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() + { + } +} +``` + +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() + { + } +} +``` + +## Inherited constraints + +- **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. + +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 { } +} +``` + +## Ambiguous match + +- **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 the error, eliminate the explicit implementation and implement both of the interface methods in the implicit implementation `public int TestMethod(int)`. + +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; + } +} +``` + +## Accessor implementation + +- **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. + +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 +} +``` + +## Not an interface + +- **CS0538**: *'name' in explicit interface declaration is not an interface* + +An attempt was made to explicitly declare an [interface](../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 + { + } +} +``` + +## Member not in interface + +- **CS0539**: *'member' in explicit interface declaration is not a member of interface* + +An attempt was made to explicitly declare an [interface](../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; + } + } +} +``` + +## Wrong location + +- **CS0541**: *'declaration' : explicit interface declaration can only be declared in a class or struct* + +An explicit [interface](../keywords/interface.md) declaration was found outside a [class](../keywords/class.md) or [struct](../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 + } +} +``` + +## Missing accessor + +- **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). + +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; } + } +} +``` + +## Accessor name conflict + +- **CS0686**: *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* + +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() {} +} +``` + +## Static implementation + +- **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](../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. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md). + +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() { } + } +} +``` + ## Incorrect member signature The following errors occur when the explicit member implementation doesn't match the declaration diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 7d9425fe62d03..a1aec41ab6d48 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -518,8 +518,8 @@ items: - name: Explicit interface implementation href: ./compiler-messages/explicit-interface-errors.md displayName: > - interface, - CS9333, CS9334 + interface, + CS0071, CS0106, CS0425, CS0460, CS0470, CS0473, CS0538, CS0539, CS0541, CS0551, CS0686, CS0736, CS9333, CS9334 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > @@ -757,8 +757,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 @@ -789,8 +787,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 @@ -1019,8 +1015,6 @@ items: href: ../misc/cs0423.md - name: CS0424 href: ../misc/cs0424.md - - name: CS0425 - href: ../misc/cs0425.md - name: CS0426 href: ../misc/cs0426.md - name: CS0428 @@ -1061,8 +1055,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 @@ -1073,8 +1065,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 @@ -1237,8 +1227,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 @@ -1307,8 +1295,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 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/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/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/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/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) From e13d09986ac5812b4a41372b0ab3ed2f12f61afa Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 13:13:46 -0500 Subject: [PATCH 03/11] expand scope to interface impl --- .openpublishing.redirection.csharp.json | 24 ++ .../explicit-interface-errors.md | 240 ++++++++++++++++++ docs/csharp/language-reference/toc.yml | 15 +- docs/csharp/misc/cs0277.md | 42 --- docs/csharp/misc/cs0535.md | 55 ---- docs/csharp/misc/cs0540.md | 60 ----- docs/csharp/misc/cs0550.md | 51 ---- docs/csharp/misc/cs0737.md | 54 ---- docs/csharp/misc/cs0738.md | 42 --- 9 files changed, 266 insertions(+), 317 deletions(-) delete mode 100644 docs/csharp/misc/cs0277.md delete mode 100644 docs/csharp/misc/cs0535.md delete mode 100644 docs/csharp/misc/cs0540.md delete mode 100644 docs/csharp/misc/cs0550.md delete mode 100644 docs/csharp/misc/cs0737.md delete mode 100644 docs/csharp/misc/cs0738.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 6c2db7dda10a9..c9f9912a95fdd 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -323,6 +323,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0106.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0277.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0178.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -371,6 +375,14 @@ "source_path_from_root": "/docs/csharp/misc/cs0736.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0737.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0738.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0767.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -1869,6 +1881,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0473.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0535.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0501.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-declarations" @@ -1905,10 +1921,18 @@ "source_path_from_root": "/docs/csharp/misc/cs0539.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0540.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0541.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0550.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0551.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md index 69c2e515f5bb5..4b56989390cf5 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -4,31 +4,43 @@ description: These compiler errors and warnings indicate errors in declaring met f1_keywords: - "CS0071" - "CS0106" + - "CS0277" - "CS0425" - "CS0460" - "CS0470" - "CS0473" + - "CS0535" - "CS0538" - "CS0539" + - "CS0540" - "CS0541" + - "CS0550" - "CS0551" - "CS0686" - "CS0736" + - "CS0737" + - "CS0738" - "CS9333" - "CS9334" helpviewer_keywords: - "CS0071" - "CS0106" + - "CS0277" - "CS0425" - "CS0460" - "CS0470" - "CS0473" + - "CS0535" - "CS0538" - "CS0539" + - "CS0540" - "CS0541" + - "CS0550" - "CS0551" - "CS0686" - "CS0736" + - "CS0737" + - "CS0738" - "CS9333" - "CS9334" ms.date: 11/12/2025 @@ -43,16 +55,22 @@ That's by design. The text closely matches the text of the compiler error / warn --> - [**CS0071**](#event-accessor-syntax): *An explicit interface implementation of an event must use event accessor syntax* - [**CS0106**](#invalid-modifiers): *The modifier 'modifier' is not valid for this item* +- [**CS0277**](#accessor-not-public): *'class' does not implement interface member 'accessor'. 'class accessor' is not public* - [**CS0425**](#constraint-mismatch): *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.* - [**CS0460**](#inherited-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* - [**CS0470**](#accessor-implementation): *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* - [**CS0473**](#ambiguous-match): *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.* +- [**CS0535**](#missing-implementation): *'class' does not implement interface member 'member'* - [**CS0538**](#not-an-interface): *'name' in explicit interface declaration is not an interface* - [**CS0539**](#member-not-in-interface): *'member' in explicit interface declaration is not a member of interface* +- [**CS0540**](#type-not-implementing-interface): *'interface member' : containing type does not implement interface 'interface'* - [**CS0541**](#wrong-location): *'declaration' : explicit interface declaration can only be declared in a class or struct* +- [**CS0550**](#extra-accessor): *'accessor' adds an accessor not found in interface member 'property'* - [**CS0551**](#missing-accessor): *Explicit interface implementation 'implementation' is missing accessor 'accessor'* - [**CS0686**](#accessor-name-conflict): *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* - [**CS0736**](#static-implementation): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* +- [**CS0737**](#method-not-public): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* +- [**CS0738**](#wrong-return-type): *'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'.* - [**CS9333**](#incorrect-member-signature): *Parameter type must match implemented member declaration.* - [**CS9334**](#incorrect-member-signature): *Return type must match implemented member declaration.* @@ -130,6 +148,37 @@ namespace MyNamespace } ``` +## Accessor not public + +- **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. + +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 the following accessor instead: + //set { } + protected set { } + } +} +``` + ## Constraint mismatch - **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.* @@ -288,6 +337,48 @@ class T } ``` +## Missing implementation + +- **CS0535**: *'class' does not implement interface member 'member'* + +A [class](../keywords/class.md) derived from an [interface](../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`. + +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() {} +} +``` + +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()) {} + } +} +``` + ## Accessor implementation - **CS0470**: *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* @@ -377,6 +468,53 @@ namespace x } ``` +## Type not implementing interface + +- **CS0540**: *'interface member' : containing type does not implement interface 'interface'* + +You attempted to implement an interface member in a [class](../keywords/class.md) that does not derive from the [interface](../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. + +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() {} +} +``` + +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()) {} + } +} +``` + ## Wrong location - **CS0541**: *'declaration' : explicit interface declaration can only be declared in a class or struct* @@ -401,6 +539,46 @@ namespace x } ``` +## Extra accessor + +- **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). + +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() {} + } +} +``` + ## Missing accessor - **CS0551**: *Explicit interface implementation 'implementation' is missing accessor 'accessor'* @@ -538,6 +716,68 @@ namespace CS0736 } ``` +## Method not public + +- **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, add the [public](../language-reference/keywords/public.md) access modifier to the method. + +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); + } + +} +``` + +## Wrong return type + +- **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, change the return type of the method to match that of the interface member. + +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(); +} +``` + ## Incorrect member signature The following errors occur when the explicit member implementation doesn't match the declaration diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index a1aec41ab6d48..138f03bef5c33 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -519,7 +519,8 @@ items: href: ./compiler-messages/explicit-interface-errors.md displayName: > interface, - CS0071, CS0106, CS0425, CS0460, CS0470, CS0473, CS0538, CS0539, CS0541, CS0551, CS0686, CS0736, CS9333, CS9334 + CS0071, CS0106, CS0277, CS0425, CS0460, CS0470, CS0473, CS0535, CS0538, CS0539, CS0540, CS0541, + CS0550, CS0551, CS0686, CS0736, CS0737, CS0738, CS9333, CS9334 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > @@ -957,8 +958,6 @@ 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 @@ -1107,16 +1106,12 @@ items: 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 @@ -1135,8 +1130,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 @@ -1295,10 +1288,6 @@ items: href: ../misc/cs0734.md - name: CS0735 href: ../misc/cs0735.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/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/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/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/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/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) From 37eff6528ac6d296b651e2dc1b1a299a10e38a3c Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 13:43:45 -0500 Subject: [PATCH 04/11] Further consolidation --- .openpublishing.redirection.csharp.json | 4 + .../explicit-interface-errors.md | 103 ++++++++++++++++++ docs/csharp/language-reference/toc.yml | 7 +- docs/csharp/misc/cs0531.md | 40 ------- ...n-t-have-specifics-on-this-csharp-error.md | 5 - 5 files changed, 110 insertions(+), 49 deletions(-) delete mode 100644 docs/csharp/misc/cs0531.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index c9f9912a95fdd..a081986001e25 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -1881,6 +1881,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0473.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0531.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0535.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md index 4b56989390cf5..93c15f798116c 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -9,6 +9,7 @@ f1_keywords: - "CS0460" - "CS0470" - "CS0473" + - "CS0531" - "CS0535" - "CS0538" - "CS0539" @@ -16,10 +17,17 @@ f1_keywords: - "CS0541" - "CS0550" - "CS0551" + - "CS0630" - "CS0686" - "CS0736" - "CS0737" - "CS0738" + - "CS8701" + - "CS8702" + - "CS8705" + - "CS8707" + - "CS8711" + - "CS8854" - "CS9333" - "CS9334" helpviewer_keywords: @@ -30,6 +38,7 @@ helpviewer_keywords: - "CS0460" - "CS0470" - "CS0473" + - "CS0531" - "CS0535" - "CS0538" - "CS0539" @@ -37,10 +46,15 @@ helpviewer_keywords: - "CS0541" - "CS0550" - "CS0551" + - "CS0630" - "CS0686" - "CS0736" - "CS0737" - "CS0738" + - "CS8705" + - "CS8707" + - "CS8711" + - "CS8854" - "CS9333" - "CS9334" ms.date: 11/12/2025 @@ -60,6 +74,7 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS0460**](#inherited-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* - [**CS0470**](#accessor-implementation): *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* - [**CS0473**](#ambiguous-match): *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-member-with-body): *'member' : interface members cannot have a definition* - [**CS0535**](#missing-implementation): *'class' does not implement interface member 'member'* - [**CS0538**](#not-an-interface): *'name' in explicit interface declaration is not an interface* - [**CS0539**](#member-not-in-interface): *'member' in explicit interface declaration is not a member of interface* @@ -67,10 +82,15 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS0541**](#wrong-location): *'declaration' : explicit interface declaration can only be declared in a class or struct* - [**CS0550**](#extra-accessor): *'accessor' adds an accessor not found in interface member 'property'* - [**CS0551**](#missing-accessor): *Explicit interface implementation 'implementation' is missing accessor 'accessor'* +- [**CS0630**](#variadic-implementation): *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* - [**CS0686**](#accessor-name-conflict): *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* - [**CS0736**](#static-implementation): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* - [**CS0737**](#method-not-public): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* - [**CS0738**](#wrong-return-type): *'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'.* +- [**CS8705**](#no-most-specific-implementation): *Interface member 'member' does not have a most specific implementation. Neither 'implementation1', nor 'implementation2' are most specific.* +- [**CS8707**](#runtime-no-support-protected-access): *Target runtime does not support 'protected', 'protected internal', or 'private protected' accessibility for a member of an interface.* +- [**CS8711**](#default-implementation-in-nopia): *Type 'type' cannot be embedded because it has a non-abstract member. Consider setting the 'Embed Interop Types' property to false.* +- [**CS8854**](#wrong-init-only): *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* - [**CS9333**](#incorrect-member-signature): *Parameter type must match implemented member declaration.* - [**CS9334**](#incorrect-member-signature): *Return type must match implemented member declaration.* @@ -337,6 +357,37 @@ class T } ``` +## Interface member with body + +- **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() + { + } + } +} +``` + ## Missing implementation - **CS0535**: *'class' does not implement interface member 'member'* @@ -614,6 +665,14 @@ public class a : ii } ``` +## Variadic implementation + +- **CS0630**: *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* + +Methods that use `__arglist` (variadic parameters) cannot implement interface members. The `__arglist` keyword allows a method to accept a variable number of arguments in an unmanaged way, but this feature is not compatible with interface implementation. + +To resolve this error, remove the `__arglist` parameter from the implementing method or use a different approach such as using `params` arrays for variable-length argument lists. + ## Accessor name conflict - **CS0686**: *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* @@ -778,6 +837,50 @@ public class Test: ITest } ``` +## No most specific implementation + +- **CS8705**: *Interface member 'member' does not have a most specific implementation. Neither 'implementation1', nor 'implementation2' are most specific.* + +This error occurs when multiple interface implementations provide a default implementation for the same member, and the compiler cannot determine which one should be used. This typically happens with diamond inheritance patterns in interfaces with default implementations. + +To resolve this error, provide an explicit implementation in the implementing class or struct to resolve the ambiguity, or restructure the interface hierarchy to avoid the conflict. + +## Runtime no support protected access + +- **CS8707**: *Target runtime does not support 'protected', 'protected internal', or 'private protected' accessibility for a member of an interface.* + +This error occurs when you use protected access modifiers on interface members (a C# 8.0 feature for default interface implementation), but the target runtime doesn't support it. + +To resolve this error, either target a runtime that supports this feature (.NET Core 3.0+, .NET 5+) or change the accessibility of the interface member to `public` or remove the implementation. + +## Default implementation in nopia + +- **CS8711**: *Type 'type' cannot be embedded because it has a non-abstract member. Consider setting the 'Embed Interop Types' property to false.* + +This error occurs when you try to embed interop types (NoPIA - No Primary Interop Assembly) that contain interfaces with default implementations. Embedded interop types cannot include non-abstract interface members. + +To resolve this error, set the 'Embed Interop Types' property to `false` for the assembly reference, or remove the default implementation from the interface. + +## Wrong init only + +- **CS8854**: *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* + +This error occurs when an interface property defines an `init` accessor, but the implementing property doesn't provide a matching `init` accessor. The `init` keyword allows property initialization during object construction but prevents modification afterward. + +To resolve this error, add an `init` accessor to the implementing property to match the interface declaration: + +```csharp +interface IExample +{ + int Value { get; init; } +} + +class Example : IExample +{ + public int Value { get; init; } // Must have init accessor +} +``` + ## Incorrect member signature The following errors occur when the explicit member implementation doesn't match the declaration diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 138f03bef5c33..6c6dfcc1c13a3 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -519,8 +519,9 @@ items: href: ./compiler-messages/explicit-interface-errors.md displayName: > interface, - CS0071, CS0106, CS0277, CS0425, CS0460, CS0470, CS0473, CS0535, CS0538, CS0539, CS0540, CS0541, - CS0550, CS0551, CS0686, CS0736, CS0737, CS0738, CS9333, CS9334 + 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: > @@ -1100,8 +1101,6 @@ 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 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/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 3aa7c8c58f535..9a73ef194e4bd 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 @@ -7,7 +7,6 @@ f1_keywords: - "CS0224" - "CS0257" - "CS0595" - - "CS0630" - "CS0847" - "CS0856" - "CS0857" @@ -339,9 +338,6 @@ f1_keywords: - "CS8700" - "CS8701" - "CS8702" - - "CS8705" - - "CS8707" - - "CS8711" - "CS8712" - "CS8715" - "CS8716" @@ -394,7 +390,6 @@ f1_keywords: - "CS8851" - "CS8852" - "CS8853" - - "CS8854" - "CS8855" - "CS8856" - "CS8857" From 44773183353a815f296b05c3933bbd189570fe63 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 14:00:29 -0500 Subject: [PATCH 05/11] partial checkin --- .../explicit-interface-errors.md | 76 +++++++++++-------- ...n-t-have-specifics-on-this-csharp-error.md | 1 + 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md index 93c15f798116c..88ad354d5f23d 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -22,11 +22,7 @@ f1_keywords: - "CS0736" - "CS0737" - "CS0738" - - "CS8701" - - "CS8702" - "CS8705" - - "CS8707" - - "CS8711" - "CS8854" - "CS9333" - "CS9334" @@ -52,8 +48,6 @@ helpviewer_keywords: - "CS0737" - "CS0738" - "CS8705" - - "CS8707" - - "CS8711" - "CS8854" - "CS9333" - "CS9334" @@ -67,33 +61,67 @@ This article covers the following compiler errors: -- [**CS0071**](#event-accessor-syntax): *An explicit interface implementation of an event must use event accessor syntax* -- [**CS0106**](#invalid-modifiers): *The modifier 'modifier' is not valid for this item* +- [**CS0071**](#interface-declaration-and-syntax): *An explicit interface implementation of an event must use event accessor syntax* +- [**CS0106**](#interface-declaration-and-syntax): *The modifier 'modifier' is not valid for this item* - [**CS0277**](#accessor-not-public): *'class' does not implement interface member 'accessor'. 'class accessor' is not public* - [**CS0425**](#constraint-mismatch): *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.* - [**CS0460**](#inherited-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* - [**CS0470**](#accessor-implementation): *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* - [**CS0473**](#ambiguous-match): *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-member-with-body): *'member' : interface members cannot have a definition* -- [**CS0535**](#missing-implementation): *'class' does not implement interface member 'member'* -- [**CS0538**](#not-an-interface): *'name' in explicit interface declaration is not an interface* +- [**CS0531**](#interface-declaration-and-syntax): *'member' : interface members cannot have a definition* +- [**CS0535**](#missing-or-incomplete-implementations): *'class' does not implement interface member 'member'* +- [**CS0538**](#interface-declaration-and-syntax): *'name' in explicit interface declaration is not an interface* - [**CS0539**](#member-not-in-interface): *'member' in explicit interface declaration is not a member of interface* - [**CS0540**](#type-not-implementing-interface): *'interface member' : containing type does not implement interface 'interface'* -- [**CS0541**](#wrong-location): *'declaration' : explicit interface declaration can only be declared in a class or struct* -- [**CS0550**](#extra-accessor): *'accessor' adds an accessor not found in interface member 'property'* -- [**CS0551**](#missing-accessor): *Explicit interface implementation 'implementation' is missing accessor 'accessor'* +- [**CS0541**](#interface-declaration-and-syntax): *'declaration' : explicit interface declaration can only be declared in a class or struct* +- [**CS0550**](#missing-or-incomplete-implementations): *'accessor' adds an accessor not found in interface member 'property'* +- [**CS0551**](#missing-or-incomplete-implementations): *Explicit interface implementation 'implementation' is missing accessor 'accessor'* - [**CS0630**](#variadic-implementation): *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* - [**CS0686**](#accessor-name-conflict): *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* - [**CS0736**](#static-implementation): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* - [**CS0737**](#method-not-public): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* - [**CS0738**](#wrong-return-type): *'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'.* - [**CS8705**](#no-most-specific-implementation): *Interface member 'member' does not have a most specific implementation. Neither 'implementation1', nor 'implementation2' are most specific.* -- [**CS8707**](#runtime-no-support-protected-access): *Target runtime does not support 'protected', 'protected internal', or 'private protected' accessibility for a member of an interface.* -- [**CS8711**](#default-implementation-in-nopia): *Type 'type' cannot be embedded because it has a non-abstract member. Consider setting the 'Embed Interop Types' property to false.* - [**CS8854**](#wrong-init-only): *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* - [**CS9333**](#incorrect-member-signature): *Parameter type must match implemented member declaration.* - [**CS9334**](#incorrect-member-signature): *Return type must match implemented member declaration.* +## 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 'modifier' is not valid for this item* +- **CS0531**: *'member' : interface members cannot have a definition* +- **CS0538**: *'name' in explicit interface declaration is not an interface* +- **CS0541**: *'declaration' : explicit interface declaration can only be declared in a class or struct* + +When explicitly implementing an [interface](../../fundamentals/types/interfaces.md) member, you must follow specific syntax rules. Understanding these rules helps avoid common declaration errors. + +For events (**CS0071**), you must manually provide `add` and `remove` event accessors when explicitly implementing an interface event. The compiler doesn't automatically generate these accessors for explicit implementations. For more information, see [How to implement interface events](../../programming-guide/events/how-to-implement-interface-events.md). + +Certain modifiers are not valid for explicit interface implementations (**CS0106**). The `public` keyword is redundant and not allowed because explicit interface implementations are implicitly public when accessed through the interface type. The `abstract` keyword is also not allowed because explicit interface implementations cannot be overridden. + +Prior to C# 8.0, interface members could not contain method bodies (**CS0531**). Starting with C# 8.0, interfaces can include [default implementations](../../whats-new/csharp-8.md#default-interface-methods) for members, allowing you to provide concrete implementations in the interface itself. + +The type specified in an explicit interface declaration must be an actual interface (**CS0538**). Attempting to use a class or other non-interface type in an explicit interface implementation syntax results in this error. + +Explicit interface declarations can only appear in classes or structs (**CS0541**). They cannot be declared at the namespace level or in other contexts. The implementing type must be a class or struct that declares the interface in its base list. + +## 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**: *'class' does not implement interface member 'member'* +- **CS0550**: *'accessor' adds an accessor not found in interface member 'property'* +- **CS0551**: *Explicit interface implementation 'implementation' is missing accessor 'accessor'* + +When a class or struct implements an [interface](../../fundamentals/types/interfaces.md), it must provide implementations for all members declared in that interface (**CS0535**). If any interface member is missing an implementation, the type must be declared as `abstract`. A class must implement all members of interfaces from which it derives unless it's marked abstract. For more information, see [Using Properties](../../programming-guide/classes-and-structs/using-properties.md). + +For property implementations, the accessors must match exactly what the interface defines. Adding an accessor that doesn't exist in the interface declaration causes **CS0550**. For example, if an interface property only declares a `get` accessor, the implementing property cannot add a `set` accessor. The implementation can only include accessors that are explicitly declared in the interface. + +Conversely, omitting an accessor that the interface requires causes **CS0551**. If an interface property declares both `get` and `set` accessors, the explicit interface implementation must provide both. Each accessor declared in the interface must have a corresponding accessor in the implementation with matching signatures. + ## Event accessor syntax - **CS0071**: *An explicit interface implementation of an event must use event accessor syntax* @@ -845,22 +873,6 @@ This error occurs when multiple interface implementations provide a default impl To resolve this error, provide an explicit implementation in the implementing class or struct to resolve the ambiguity, or restructure the interface hierarchy to avoid the conflict. -## Runtime no support protected access - -- **CS8707**: *Target runtime does not support 'protected', 'protected internal', or 'private protected' accessibility for a member of an interface.* - -This error occurs when you use protected access modifiers on interface members (a C# 8.0 feature for default interface implementation), but the target runtime doesn't support it. - -To resolve this error, either target a runtime that supports this feature (.NET Core 3.0+, .NET 5+) or change the accessibility of the interface member to `public` or remove the implementation. - -## Default implementation in nopia - -- **CS8711**: *Type 'type' cannot be embedded because it has a non-abstract member. Consider setting the 'Embed Interop Types' property to false.* - -This error occurs when you try to embed interop types (NoPIA - No Primary Interop Assembly) that contain interfaces with default implementations. Embedded interop types cannot include non-abstract interface members. - -To resolve this error, set the 'Embed Interop Types' property to `false` for the assembly reference, or remove the default implementation from the interface. - ## Wrong init only - **CS8854**: *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* 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 9a73ef194e4bd..bbb3152f2f810 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 @@ -338,6 +338,7 @@ f1_keywords: - "CS8700" - "CS8701" - "CS8702" + - "CS8707" - "CS8712" - "CS8715" - "CS8716" From eaf51d32841934ee3f181d2c5c716e97b05679ed Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 14:16:47 -0500 Subject: [PATCH 06/11] First pass on themes. --- .../explicit-interface-errors.md | 880 +++++++----------- 1 file changed, 354 insertions(+), 526 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md index 88ad354d5f23d..a355831a94612 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -63,28 +63,28 @@ That's by design. The text closely matches the text of the compiler error / warn --> - [**CS0071**](#interface-declaration-and-syntax): *An explicit interface implementation of an event must use event accessor syntax* - [**CS0106**](#interface-declaration-and-syntax): *The modifier 'modifier' is not valid for this item* -- [**CS0277**](#accessor-not-public): *'class' does not implement interface member 'accessor'. 'class accessor' is not public* -- [**CS0425**](#constraint-mismatch): *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.* -- [**CS0460**](#inherited-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* -- [**CS0470**](#accessor-implementation): *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* -- [**CS0473**](#ambiguous-match): *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.* +- [**CS0277**](#accessor-implementation-and-conflicts): *'class' does not implement interface member 'accessor'. 'class accessor' is not public* +- [**CS0425**](#generic-type-constraints): *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.* +- [**CS0460**](#generic-type-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* +- [**CS0470**](#accessor-implementation-and-conflicts): *Method 'method' cannot implement interface accessor 'accessor' for type '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): *'member' : interface members cannot have a definition* - [**CS0535**](#missing-or-incomplete-implementations): *'class' does not implement interface member 'member'* - [**CS0538**](#interface-declaration-and-syntax): *'name' in explicit interface declaration is not an interface* -- [**CS0539**](#member-not-in-interface): *'member' in explicit interface declaration is not a member of interface* -- [**CS0540**](#type-not-implementing-interface): *'interface member' : containing type does not implement interface 'interface'* +- [**CS0539**](#member-matching-and-resolution): *'member' in explicit interface declaration is not a member of interface* +- [**CS0540**](#member-matching-and-resolution): *'interface member' : containing type does not implement interface 'interface'* - [**CS0541**](#interface-declaration-and-syntax): *'declaration' : explicit interface declaration can only be declared in a class or struct* - [**CS0550**](#missing-or-incomplete-implementations): *'accessor' adds an accessor not found in interface member 'property'* - [**CS0551**](#missing-or-incomplete-implementations): *Explicit interface implementation 'implementation' is missing accessor 'accessor'* -- [**CS0630**](#variadic-implementation): *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* -- [**CS0686**](#accessor-name-conflict): *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* -- [**CS0736**](#static-implementation): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* -- [**CS0737**](#method-not-public): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* -- [**CS0738**](#wrong-return-type): *'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'.* -- [**CS8705**](#no-most-specific-implementation): *Interface member 'member' does not have a most specific implementation. Neither 'implementation1', nor 'implementation2' are most specific.* -- [**CS8854**](#wrong-init-only): *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* -- [**CS9333**](#incorrect-member-signature): *Parameter type must match implemented member declaration.* -- [**CS9334**](#incorrect-member-signature): *Return type must match implemented member declaration.* +- [**CS0630**](#special-implementation-restrictions): *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* +- [**CS0686**](#accessor-implementation-and-conflicts): *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* +- [**CS0736**](#method-visibility-and-modifiers): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* +- [**CS0737**](#method-visibility-and-modifiers): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* +- [**CS0738**](#return-types-and-signatures): *'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'.* +- [**CS8705**](#ambiguous-and-conflicting-implementations): *Interface member 'member' does not have a most specific implementation. Neither 'implementation1', nor 'implementation2' are most specific.* +- [**CS8854**](#return-types-and-signatures): *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* +- [**CS9333**](#return-types-and-signatures): *Parameter type must match implemented member declaration.* +- [**CS9334**](#return-types-and-signatures): *Return type must match implemented member declaration.* ## Interface declaration and syntax @@ -116,93 +116,106 @@ The following errors occur when a class fails to fully implement an interface or - **CS0550**: *'accessor' adds an accessor not found in interface member 'property'* - **CS0551**: *Explicit interface implementation 'implementation' is missing accessor 'accessor'* -When a class or struct implements an [interface](../../fundamentals/types/interfaces.md), it must provide implementations for all members declared in that interface (**CS0535**). If any interface member is missing an implementation, the type must be declared as `abstract`. A class must implement all members of interfaces from which it derives unless it's marked abstract. For more information, see [Using Properties](../../programming-guide/classes-and-structs/using-properties.md). +When a class or struct implements an [interface](../../fundamentals/types/interfaces.md), it must provide implementations for all members declared in that interface. If any interface member is missing an implementation, the type must be declared as `abstract`. A class must implement all members of interfaces from which it derives unless it's marked abstract. -For property implementations, the accessors must match exactly what the interface defines. Adding an accessor that doesn't exist in the interface declaration causes **CS0550**. For example, if an interface property only declares a `get` accessor, the implementing property cannot add a `set` accessor. The implementation can only include accessors that are explicitly declared in the interface. +For property implementations, the accessors must match exactly what the interface defines. Adding an accessor that doesn't exist in the interface declaration causes an error. For example, if an interface property only declares a `get` accessor, the implementing property cannot add a `set` accessor. The implementation can only include accessors that are explicitly declared in the interface. -Conversely, omitting an accessor that the interface requires causes **CS0551**. If an interface property declares both `get` and `set` accessors, the explicit interface implementation must provide both. Each accessor declared in the interface must have a corresponding accessor in the implementation with matching signatures. +Conversely, omitting an accessor that the interface requires also causes an error. If an interface property declares both `get` and `set` accessors, the explicit interface implementation must provide both. Each accessor declared in the interface must have a corresponding accessor in the implementation with matching signatures. -## Event accessor syntax - -- **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). - -The following sample generates CS0071: +The following example shows both missing implementation (**CS0535**) and correct implementation: ```csharp -// CS0071.cs -public delegate void MyEvent(object sender); - -interface ITest +// CS0535.cs +public interface A { - event MyEvent Clicked; + void F(); } -class Test : ITest -{ - event MyEvent ITest.Clicked; // CS0071 - - // Try the following code instead. - /* - private MyEvent clicked; +public class B : A {} // CS0535 A::F is not implemented - event MyEvent ITest.Clicked - { - add - { - clicked += value; - } - remove - { - clicked -= value; - } - } - */ - public static void Main() { } +// OK +public class C : A { + public void F() {} + public static void Main() {} } ``` -## Invalid modifiers - -- **CS0106**: *The modifier 'modifier' is not valid for this item* +The following example demonstrates adding an extra accessor (**CS0550**): -When declaring explicit interface implementations, certain modifiers are not allowed: +```csharp +// CS0550.cs +namespace x +{ + interface ii + { + int i + { + get; + // add the following accessor to resolve this CS0550 + // set; + } + } -- 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. + public class a : ii + { + int ii.i + { + get + { + return 0; + } + set {} // CS0550 no set in interface + } -For more information, see [Interfaces](../../fundamentals/types/interfaces.md). + public static void Main() {} + } +} +``` -The following sample generates CS0106: +The following example demonstrates missing an accessor (**CS0551**): ```csharp -// CS0106.cs -namespace MyNamespace +// CS0551.cs +// compile with: /target:library +interface ii { - interface I + int i { - void M1(); - void M2(); + get; + set; } +} - public class MyClass : I - { - public void I.M1() {} // CS0106 - abstract void I.M2() {} // CS0106 +public class a : ii +{ + int ii.i { set {} } // CS0551 - public static void Main() {} + // OK + int ii.i + { + set {} + get { return 0; } } } ``` -## Accessor not public +For more information, see [Using Properties](../../programming-guide/classes-and-structs/using-properties.md) and [Interfaces](../../fundamentals/types/interfaces.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**: *'class' does not implement interface member 'accessor'. 'class accessor' is not public* +- **CS0470**: *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* +- **CS0686**: *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* + +When implementing interface properties, accessors must have public accessibility. Property accessor implementations in a class that don't have public accessibility cause **CS0277**. All interface members are `public`, so any implementing accessor must also be 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. +The **CS0470** error occurs when you try to use a method with an accessor-like name (such as `get_PropertyName`) to implement an interface property. You must use proper property syntax with explicit interface implementation instead. -The following example generates CS0277: +The **CS0686** 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. + +The following example demonstrates **CS0277** with a non-public accessor: ```csharp // CS0277.cs @@ -227,13 +240,174 @@ public class MyClass : MyInterface // CS0277 } ``` -## Constraint mismatch +The following example demonstrates **CS0470** with improper accessor implementation: + +```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 +} +``` + +The following example demonstrates **CS0686** with property name conflicts: + +```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() {} +} +``` + +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: + +```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() {} +} +``` + +For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md) and [Events](../../programming-guide/events/index.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 a member of interface* +- **CS0540**: *'interface member' : containing type does not implement interface 'interface'* + +When you explicitly implement an interface member, the member you're implementing must actually exist in the interface. Attempting to implement a member that isn't declared in the interface causes **CS0539**. Verify that the member name and signature match exactly what's declared in the interface, or remove the incorrect implementation. + +The **CS0540** error occurs when you try to explicitly implement an interface member in a type that doesn't declare that interface in its base list. To resolve this error, either add the interface to the class's base list, or remove the explicit interface implementation. + +The following example demonstrates **CS0539** where a method name doesn't match: + +```csharp +// CS0539.cs +namespace x +{ + interface I + { + void m(); + } + + public class clx : I + { + void I.x() // CS0539 - 'x' is not in interface I + { + } + + public static int Main() + { + return 0; + } + } +} +``` + +The following examples demonstrate **CS0540** where the type doesn't implement the interface: + +```csharp +// CS0540.cs +interface I +{ + void m(); +} + +public class Clx +{ + void I.m() {} // CS0540 - Clx doesn't implement I +} + +// OK +public class Cly : I +{ + void I.m() {} + public static void Main() {} +} +``` + +```csharp +// CS0540_b.cs +using System; +class C { + void IDisposable.Dispose() {} // CS0540 - C doesn't implement IDisposable +} + +class D : IDisposable { + void IDisposable.Dispose() {} + public void Dispose() {} + + static void Main() { + using (D d = new D()) {} + } +} +``` + +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 '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.* +- **CS0460**: *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* -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. +When implementing generic interface methods or overriding virtual generic methods, the type parameter constraints must match those defined in the interface or base method. The **CS0425** error occurs when a method implementation has different constraints than the interface method. To avoid this error, ensure the `where` clause is identical in both declarations, or use explicit interface implementation. -The following example generates CS0425: +The constraints don't have to be a literal match, as long as the set of constraints has the same meaning. For example, equivalent constraints like `where T : J, J` and `where X : J` (when S is int) are acceptable. + +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 (**CS0460**). 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. + +The following example demonstrates **CS0425** with mismatched constraints: ```csharp // CS0425.cs @@ -266,7 +440,7 @@ class CMain } ``` -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: +The following example shows equivalent constraints that are acceptable: ```csharp // CS0425b.cs @@ -292,18 +466,7 @@ class C : I } ``` -## Inherited constraints - -- **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. - -The following sample generates CS0460 when attempting to redeclare inherited constraints: +The following example demonstrates **CS0460** when attempting to redeclare inherited constraints: ```csharp // CS0460.cs @@ -337,565 +500,230 @@ class ExpImpl : I } ``` -## Ambiguous match +For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md) and [Interfaces](../../fundamentals/types/interfaces.md). -- **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.* +## Method visibility and modifiers -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. +The following errors occur when implementing interface methods with incorrect accessibility or modifiers: -To correct the error, eliminate the explicit implementation and implement both of the interface methods in the implicit implementation `public int TestMethod(int)`. +- **CS0736**: *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* +- **CS0737**: *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* -The following code generates CS0473: +Methods that implement interface members must have public accessibility and cannot be static. The **CS0736** error is generated when a static method is either implicitly or explicitly declared as an implementation of an interface member. Interface members are instance members by design, so static methods cannot fulfill the interface contract. -```csharp -public interface ITest -{ - int TestMethod(int i); - int TestMethod(T i); -} +The **CS0737** error occurs when a method that implements an interface member doesn't have public accessibility. All interface members are `public`, so the implementing method must also be public. -public class ImplementingClass : ITest +To correct **CS0736**, remove the `static` modifier from the method declaration, change the name of the interface method, or redefine the containing type so that it doesn't inherit from the interface. + +To correct **CS0737**, add the `public` access modifier to the method. + +The following example demonstrates **CS0736** with a static method: + +```csharp +// cs0736.cs +namespace CS0736 { - int ITest.TestMethod(int i) // CS0473 + interface ITest { - return i + 1; + int testMethod(int x); } - public int TestMethod(int i) + class Program : ITest // CS0736 { - return i - 1; + public static int testMethod(int x) { return 0; } + // Try the following line instead. + // public int testMethod(int x) { return 0; } + public static void Main() { } } } +``` -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; - } -} -``` - -## Interface member with body - -- **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: +The following example demonstrates **CS0737** with a non-public method: ```csharp -// CS0531.cs -namespace x +// cs0737.cs +interface ITest { - 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() - { - } - } + // Default access of private with no modifier. + int Return42(); + // Try the following line instead. + // public int Return42(); } -``` -## Missing implementation - -- **CS0535**: *'class' does not implement interface member 'member'* - -A [class](../keywords/class.md) derived from an [interface](../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`. - -The following sample generates CS0535: - -```csharp -// CS0535.cs -public interface A +struct Struct1 : ITest // CS0737 { - void F(); -} - -public class B : A {} // CS0535 A::F is not implemented - -// OK -public class C : A { - public void F() {} - public static void Main() {} + int Return42() { return (42); } } -``` - -The following sample generates CS0535: -```csharp -// CS0535_b.cs -using System; -class C : IDisposable {} // CS0535 +public class Test +{ + public static int Main(string[] args) + { + Struct1 s1 = new Struct1(); -// OK -class D : IDisposable { - void IDisposable.Dispose() {} - public void Dispose() {} + return (1); + } - static void Main() { - using (D d = new D()) {} - } } ``` -## Accessor implementation - -- **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. +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md). -The following sample generates CS0470: +## Ambiguous and conflicting implementations -```csharp -// CS0470.cs -// compile with: /target:library +The following errors occur when the compiler cannot determine which interface implementation to use: -interface I -{ - int P { get; } -} +- **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 'implementation1', nor 'implementation2' are most specific.* -class MyClass : I -{ - public int get_P() { return 0; } // CS0470 - public int P2 { get { return 0;} } // OK -} -``` +The **CS0473** error occurs when a generic method might acquire the same signature as a non-generic method. In some cases, there's no way in the common language infrastructure (CLI) metadata system to unambiguously state which method binds to which slot. It's up to the CLI to make that determination. -## Not an interface +To correct this error, eliminate the explicit implementation and implement both of the interface methods in the implicit implementation. -- **CS0538**: *'name' in explicit interface declaration is not an interface* +The **CS8705** error occurs when multiple interface implementations provide a default implementation for the same member, and the compiler cannot determine which one should be used. This typically happens with diamond inheritance patterns in interfaces with default implementations. -An attempt was made to explicitly declare an [interface](../keywords/interface.md), but an interface was not specified. +To resolve **CS8705**, provide an explicit implementation in the implementing class or struct to resolve the ambiguity, or restructure the interface hierarchy to avoid the conflict. -The following sample generates CS0538: +The following example demonstrates **CS0473**: ```csharp -// CS0538.cs -interface MyIFace -{ - void F(); -} - -public class MyClass +public interface ITest { - public void G() - { - } + int TestMethod(int i); + int TestMethod(T i); } -class C: MyIFace +public class ImplementingClass : ITest { - void MyIFace.F() - { - } + int ITest.TestMethod(int i) // CS0473 + { + return i + 1; + } - void MyClass.G() // CS0538, MyClass not an interface - { - } + public int TestMethod(int i) + { + return i - 1; + } } -``` - -## Member not in interface - -- **CS0539**: *'member' in explicit interface declaration is not a member of interface* - -An attempt was made to explicitly declare an [interface](../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 +class T { - interface I - { - void m(); - } + static int Main() + { + ImplementingClass a = new ImplementingClass(); + if (a.TestMethod(0) != -1) + return -1; - public class clx : I - { - void I.x() // CS0539 - { - } + ITest i_a = a; + System.Console.WriteLine(i_a.TestMethod(0).ToString()); + if (i_a.TestMethod(0) != 1) + return -1; - public static int Main() - { - return 0; - } - } + return 0; + } } ``` -## Type not implementing interface +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Default Interface Methods](../../whats-new/csharp-8.md#default-interface-methods). -- **CS0540**: *'interface member' : containing type does not implement interface 'interface'* +## Return types and signatures -You attempted to implement an interface member in a [class](../keywords/class.md) that does not derive from the [interface](../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. +The following errors occur when the implementing method's signature doesn't match the interface member declaration: -The following sample generates CS0540: +- **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'.* +- **CS8854**: *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* +- **CS9333**: *Parameter type must match implemented member declaration.* +- **CS9334**: *Return type must match implemented member declaration.* -```csharp -// CS0540.cs -interface I -{ - void m(); -} +When implementing interface members, the signature must match exactly. The return type of an implementing method must match the return type of the interface member (**CS0738**). Similarly, parameter types (**CS9333**) and return types (**CS9334**) must match the interface declaration exactly. -public class Clx -{ - void I.m() {} // CS0540 -} +The **CS8854** error occurs when an interface property defines an `init` accessor, but the implementing property doesn't provide a matching `init` accessor. The `init` keyword allows property initialization during object construction but prevents modification afterward. To resolve this error, add an `init` accessor to the implementing property to match the interface declaration. -// OK -public class Cly : I -{ - void I.m() {} - public static void Main() {} -} -``` +To correct **CS0738**, change the return type of the method to match that of the interface member. To correct **CS9333** and **CS9334**, ensure the parameter types and return types match the interface member declaration exactly. -The following sample generates CS0540: +The following example demonstrates **CS0738** with mismatched return types: ```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()) {} - } -} -``` - -## Wrong location - -- **CS0541**: *'declaration' : explicit interface declaration can only be declared in a class or struct* - -An explicit [interface](../keywords/interface.md) declaration was found outside a [class](../keywords/class.md) or [struct](../builtin-types/struct.md). -The following sample generates CS0541: - -```csharp -// CS0541.cs -namespace x +interface ITest { - interface IFace - { - void F(); - } - - interface IFace2 : IFace - { - void IFace.F(); // CS0541 - } + int TestMethod(); } -``` - -## Extra accessor - -- **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). - -The following sample generates CS0550: - -```csharp -// CS0550.cs -namespace x +public class Test: ITest { - 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() {} - } + public void TestMethod() { } // CS0738 + // Try the following line instead. + // public int TestMethod(); } ``` -## Missing accessor - -- **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). - -The following sample generates CS0551: +The following example demonstrates **CS8854** with missing `init` accessor: ```csharp -// CS0551.cs -// compile with: /target:library -interface ii +interface IExample { - int i - { - get; - set; - } + int Value { get; init; } } -public class a : ii +class Example : IExample { - int ii.i { set {} } // CS0551 - - // OK - int ii.i - { - set {} - get { return 0; } - } + public int Value { get; init; } // Must have init accessor } ``` -## Variadic implementation - -- **CS0630**: *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* - -Methods that use `__arglist` (variadic parameters) cannot implement interface members. The `__arglist` keyword allows a method to accept a variable number of arguments in an unmanaged way, but this feature is not compatible with interface implementation. - -To resolve this error, remove the `__arglist` parameter from the implementing method or use a different approach such as using `params` arrays for variable-length argument lists. - -## Accessor name conflict - -- **CS0686**: *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* - -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 +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Init-only setters](../../whats-new/csharp-9.md#init-only-setters). -The following sample generates CS0686: +## Special implementation restrictions -```csharp -// CS0686.cs -interface I -{ - int get_P(); -} +The following error occurs when using special parameter types that aren't compatible with interface implementation: -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() {} -} -``` +- **CS0630**: *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* -### Example 2 +Methods that use `__arglist` (variadic parameters) cannot implement interface members. The `__arglist` keyword allows a method to accept a variable number of arguments in an unmanaged way, but this feature is not compatible with interface implementation. This is a limitation of how interface contracts work—they require predictable, type-safe signatures. -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: +To resolve this error, remove the `__arglist` parameter from the implementing method or use a different approach such as using `params` arrays for variable-length argument lists, which are compatible with interface implementation. -```csharp -// CS0686b.cs -using System; +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [params keyword](../keywords/method-parameters.md#params-modifier). -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() {} -} -``` -## Static implementation -- **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](../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. -For more information, see [Interfaces](../../fundamentals/types/interfaces.md). -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() { } - } -} -``` -## Method not public -- **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, add the [public](../language-reference/keywords/public.md) access modifier to the method. -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); - } -} -``` -## Wrong return type -- **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, change the return type of the method to match that of the interface member. -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(); -} -``` -## No most specific implementation -- **CS8705**: *Interface member 'member' does not have a most specific implementation. Neither 'implementation1', nor 'implementation2' are most specific.* -This error occurs when multiple interface implementations provide a default implementation for the same member, and the compiler cannot determine which one should be used. This typically happens with diamond inheritance patterns in interfaces with default implementations. -To resolve this error, provide an explicit implementation in the implementing class or struct to resolve the ambiguity, or restructure the interface hierarchy to avoid the conflict. -## Wrong init only -- **CS8854**: *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* -This error occurs when an interface property defines an `init` accessor, but the implementing property doesn't provide a matching `init` accessor. The `init` keyword allows property initialization during object construction but prevents modification afterward. -To resolve this error, add an `init` accessor to the implementing property to match the interface declaration: -```csharp -interface IExample -{ - int Value { get; init; } -} -class Example : IExample -{ - public int Value { get; init; } // Must have init accessor -} -``` -## Incorrect member signature -The following errors occur when the explicit member implementation doesn't match the declaration -- **CS9333**: *Parameter type must match implemented member declaration.* -- **CS9334**: *Return type must match implemented member declaration.* From 8c7807b6f1f94f1e8b9dc4c1862e29904fcea756 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 14:28:14 -0500 Subject: [PATCH 07/11] First edit pass --- .../explicit-interface-errors.md | 554 ++---------------- 1 file changed, 48 insertions(+), 506 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md index a355831a94612..b97e21c96bc72 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -96,17 +96,16 @@ The following errors relate to proper syntax and structure when declaring explic - **CS0538**: *'name' in explicit interface declaration is not an interface* - **CS0541**: *'declaration' : explicit interface declaration can only be declared in a class or struct* -When explicitly implementing an [interface](../../fundamentals/types/interfaces.md) member, you must follow specific syntax rules. Understanding these rules helps avoid common declaration errors. +You can correct these errors using the following techniques: -For events (**CS0071**), you must manually provide `add` and `remove` event accessors when explicitly implementing an interface event. The compiler doesn't automatically generate these accessors for explicit implementations. For more information, see [How to implement interface events](../../programming-guide/events/how-to-implement-interface-events.md). +- 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 cannot be marked as abstract because they cannot 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**). Prior to C# 8.0, interface members cannot contain implementations; starting with C# 8.0, you can provide [default interface methods](../../whats-new/csharp-8.md#default-interface-methods) 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 cannot be declared at the namespace level or in other contexts. -Certain modifiers are not valid for explicit interface implementations (**CS0106**). The `public` keyword is redundant and not allowed because explicit interface implementations are implicitly public when accessed through the interface type. The `abstract` keyword is also not allowed because explicit interface implementations cannot be overridden. - -Prior to C# 8.0, interface members could not contain method bodies (**CS0531**). Starting with C# 8.0, interfaces can include [default implementations](../../whats-new/csharp-8.md#default-interface-methods) for members, allowing you to provide concrete implementations in the interface itself. - -The type specified in an explicit interface declaration must be an actual interface (**CS0538**). Attempting to use a class or other non-interface type in an explicit interface implementation syntax results in this error. - -Explicit interface declarations can only appear in classes or structs (**CS0541**). They cannot be declared at the namespace level or in other contexts. The implementing type must be a class or struct that declares the interface in its base list. +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). ## Missing or incomplete implementations @@ -116,90 +115,13 @@ The following errors occur when a class fails to fully implement an interface or - **CS0550**: *'accessor' adds an accessor not found in interface member 'property'* - **CS0551**: *Explicit interface implementation 'implementation' is missing accessor 'accessor'* -When a class or struct implements an [interface](../../fundamentals/types/interfaces.md), it must provide implementations for all members declared in that interface. If any interface member is missing an implementation, the type must be declared as `abstract`. A class must implement all members of interfaces from which it derives unless it's marked abstract. - -For property implementations, the accessors must match exactly what the interface defines. Adding an accessor that doesn't exist in the interface declaration causes an error. For example, if an interface property only declares a `get` accessor, the implementing property cannot add a `set` accessor. The implementation can only include accessors that are explicitly declared in the interface. - -Conversely, omitting an accessor that the interface requires also causes an error. If an interface property declares both `get` and `set` accessors, the explicit interface implementation must provide both. Each accessor declared in the interface must have a corresponding accessor in the implementation with matching signatures. - -The following example shows both missing implementation (**CS0535**) and correct implementation: - -```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() {} -} -``` - -The following example demonstrates adding an extra accessor (**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() {} - } -} -``` - -The following example demonstrates missing an accessor (**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; } - } -} -``` - -For more information, see [Using Properties](../../programming-guide/classes-and-structs/using-properties.md) and [Interfaces](../../fundamentals/types/interfaces.md). +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**). When a class or struct declares that it implements an interface, it establishes a contract to provide concrete implementations for all interface members, so 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). ## Accessor implementation and conflicts @@ -209,108 +131,13 @@ The following errors occur when implementing interface properties or events with - **CS0470**: *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* - **CS0686**: *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* -When implementing interface properties, accessors must have public accessibility. Property accessor implementations in a class that don't have public accessibility cause **CS0277**. All interface members are `public`, so any implementing accessor must also be public. - -The **CS0470** error occurs when you try to use a method with an accessor-like name (such as `get_PropertyName`) to implement an interface property. You must use proper property syntax with explicit interface implementation instead. - -The **CS0686** 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. - -The following example demonstrates **CS0277** with a non-public accessor: - -```csharp -// CS0277.cs -public interface MyInterface -{ - int Property - { - get; - set; - } -} - -public class MyClass : MyInterface // CS0277 -{ - public int Property - { - get { return 0; } - // Try the following accessor instead: - //set { } - protected set { } - } -} -``` - -The following example demonstrates **CS0470** with improper accessor implementation: - -```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 -} -``` - -The following example demonstrates **CS0686** with property name conflicts: - -```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() {} -} -``` - -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: - -```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() {} -} -``` - -For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md) and [Events](../../programming-guide/events/index.md). +You can correct these errors using the following techniques: + +- Remove any access modifiers from property accessors that restrict visibility below `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). ## Member matching and resolution @@ -319,73 +146,10 @@ The following errors occur when attempting to implement interface members that d - **CS0539**: *'member' in explicit interface declaration is not a member of interface* - **CS0540**: *'interface member' : containing type does not implement interface 'interface'* -When you explicitly implement an interface member, the member you're implementing must actually exist in the interface. Attempting to implement a member that isn't declared in the interface causes **CS0539**. Verify that the member name and signature match exactly what's declared in the interface, or remove the incorrect implementation. - -The **CS0540** error occurs when you try to explicitly implement an interface member in a type that doesn't declare that interface in its base list. To resolve this error, either add the interface to the class's base list, or remove the explicit interface implementation. - -The following example demonstrates **CS0539** where a method name doesn't match: - -```csharp -// CS0539.cs -namespace x -{ - interface I - { - void m(); - } - - public class clx : I - { - void I.x() // CS0539 - 'x' is not in interface I - { - } - - public static int Main() - { - return 0; - } - } -} -``` - -The following examples demonstrate **CS0540** where the type doesn't implement the interface: - -```csharp -// CS0540.cs -interface I -{ - void m(); -} - -public class Clx -{ - void I.m() {} // CS0540 - Clx doesn't implement I -} - -// OK -public class Cly : I -{ - void I.m() {} - public static void Main() {} -} -``` - -```csharp -// CS0540_b.cs -using System; -class C { - void IDisposable.Dispose() {} // CS0540 - C doesn't implement IDisposable -} - -class D : IDisposable { - void IDisposable.Dispose() {} - public void Dispose() {} - - static void Main() { - using (D d = new D()) {} - } -} -``` +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). @@ -396,111 +160,14 @@ The following errors occur when implementing generic interface methods with type - **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.* - **CS0460**: *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* -When implementing generic interface methods or overriding virtual generic methods, the type parameter constraints must match those defined in the interface or base method. The **CS0425** error occurs when a method implementation has different constraints than the interface method. To avoid this error, ensure the `where` clause is identical in both declarations, or use explicit interface implementation. - -The constraints don't have to be a literal match, as long as the set of constraints has the same meaning. For example, equivalent constraints like `where T : J, J` and `where X : J` (when S is int) are acceptable. - -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 (**CS0460**). 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. - -The following example demonstrates **CS0425** with mismatched constraints: - -```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() - { - } -} -``` - -The following example shows equivalent constraints that are acceptable: - -```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() - { - } -} -``` - -The following example demonstrates **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 { } -} -``` - -For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md) and [Interfaces](../../fundamentals/types/interfaces.md). +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 because the constraints are part of the method signature contract, although the constraints don't have to be literally identical as long as they express the same set of requirements. +- 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 @@ -509,63 +176,10 @@ The following errors occur when implementing interface methods with incorrect ac - **CS0736**: *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* - **CS0737**: *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* -Methods that implement interface members must have public accessibility and cannot be static. The **CS0736** error is generated when a static method is either implicitly or explicitly declared as an implementation of an interface member. Interface members are instance members by design, so static methods cannot fulfill the interface contract. - -The **CS0737** error occurs when a method that implements an interface member doesn't have public accessibility. All interface members are `public`, so the implementing method must also be public. - -To correct **CS0736**, remove the `static` modifier from the method declaration, change the name of the interface method, or redefine the containing type so that it doesn't inherit from the interface. - -To correct **CS0737**, add the `public` access modifier to the method. - -The following example demonstrates **CS0736** with a static method: - -```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() { } - } -} -``` - -The following example demonstrates **CS0737** with a non-public method: - -```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); } -} +You can correct these errors using the following techniques: -public class Test -{ - public static int Main(string[] args) - { - Struct1 s1 = new Struct1(); - - return (1); - } - -} -``` +- Remove the `static` modifier from the method declaration that implements the interface member (**CS0736**). Interface members are instance members by design and must be invoked on an instance of the implementing type, so static methods cannot fulfill the interface contract because they belong to the type itself rather than to instances of the type. +- 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). @@ -576,53 +190,11 @@ The following errors occur when the compiler cannot determine which interface im - **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 'implementation1', nor 'implementation2' are most specific.* -The **CS0473** error occurs when a generic method might acquire the same signature as a non-generic method. In some cases, there's no way in the common language infrastructure (CLI) metadata system to unambiguously state which method binds to which slot. It's up to the CLI to make that determination. - -To correct this error, eliminate the explicit implementation and implement both of the interface methods in the implicit implementation. - -The **CS8705** error occurs when multiple interface implementations provide a default implementation for the same member, and the compiler cannot determine which one should be used. This typically happens with diamond inheritance patterns in interfaces with default implementations. - -To resolve **CS8705**, provide an explicit implementation in the implementing class or struct to resolve the ambiguity, or restructure the interface hierarchy to avoid the conflict. - -The following example demonstrates **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; - } -} +You can correct these errors using the following techniques: -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; - } -} -``` +- 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 cannot 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, and 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](../../whats-new/csharp-8.md#default-interface-methods). @@ -635,42 +207,11 @@ The following errors occur when the implementing method's signature doesn't matc - **CS9333**: *Parameter type must match implemented member declaration.* - **CS9334**: *Return type must match implemented member declaration.* -When implementing interface members, the signature must match exactly. The return type of an implementing method must match the return type of the interface member (**CS0738**). Similarly, parameter types (**CS9333**) and return types (**CS9334**) must match the interface declaration exactly. - -The **CS8854** error occurs when an interface property defines an `init` accessor, but the implementing property doesn't provide a matching `init` accessor. The `init` keyword allows property initialization during object construction but prevents modification afterward. To resolve this error, add an `init` accessor to the implementing property to match the interface declaration. - -To correct **CS0738**, change the return type of the method to match that of the interface member. To correct **CS9333** and **CS9334**, ensure the parameter types and return types match the interface member declaration exactly. - -The following example demonstrates **CS0738** with mismatched return types: - -```csharp -using System; - -interface ITest -{ - int TestMethod(); -} -public class Test: ITest -{ - public void TestMethod() { } // CS0738 - // Try the following line instead. - // public int TestMethod(); -} -``` - -The following example demonstrates **CS8854** with missing `init` accessor: - -```csharp -interface IExample -{ - int Value { get; init; } -} +You can correct these errors using the following techniques: -class Example : IExample -{ - public int Value { get; init; } // Must have init accessor -} -``` +- 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](../../whats-new/csharp-9.md#init-only-setters). @@ -680,9 +221,10 @@ The following error occurs when using special parameter types that aren't compat - **CS0630**: *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* -Methods that use `__arglist` (variadic parameters) cannot implement interface members. The `__arglist` keyword allows a method to accept a variable number of arguments in an unmanaged way, but this feature is not compatible with interface implementation. This is a limitation of how interface contracts work—they require predictable, type-safe signatures. +You can correct this error using the following techniques: -To resolve this error, remove the `__arglist` parameter from the implementing method or use a different approach such as using `params` arrays for variable-length argument lists, which are compatible with interface implementation. +- 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). From db1a15c843f5fe210f31a81beadabfce7f57e9c4 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 15:04:34 -0500 Subject: [PATCH 08/11] second edit pass. --- .github/prompts/error-consolidation.md | 11 ++++ .../explicit-interface-errors.md | 58 +++++++++---------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index e7fabb653e4d1..4e94dec8d9845 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -66,3 +66,14 @@ Understand these instructions, then suggest a list of themes and the included er 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. +## 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 error instance of the same message matches exactly. diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md index b97e21c96bc72..aa5dd81604468 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md @@ -62,29 +62,29 @@ This article covers the following compiler errors: That's by design. The text closely matches the text of the compiler error / warning for SEO purposes. --> - [**CS0071**](#interface-declaration-and-syntax): *An explicit interface implementation of an event must use event accessor syntax* -- [**CS0106**](#interface-declaration-and-syntax): *The modifier 'modifier' is not valid for this item* -- [**CS0277**](#accessor-implementation-and-conflicts): *'class' does not implement interface member 'accessor'. 'class accessor' is not public* -- [**CS0425**](#generic-type-constraints): *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.* -- [**CS0460**](#generic-type-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* -- [**CS0470**](#accessor-implementation-and-conflicts): *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* +- [**CS0106**](#interface-declaration-and-syntax): *The modifier '{0}' is not valid for this item* +- [**CS0277**](#accessor-implementation-and-conflicts): *'{0}' does not implement interface member '{1}'. '{2}' is not public.* +- [**CS0425**](#generic-type-constraints): *The constraints for type parameter '{0}' of method '{1}' must match the constraints for type parameter '{2}' of interface method '{3}'. 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 '{0}' cannot implement interface accessor '{1}' for type '{2}'. 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): *'member' : interface members cannot have a definition* -- [**CS0535**](#missing-or-incomplete-implementations): *'class' does not implement interface member 'member'* -- [**CS0538**](#interface-declaration-and-syntax): *'name' in explicit interface declaration is not an interface* -- [**CS0539**](#member-matching-and-resolution): *'member' in explicit interface declaration is not a member of interface* -- [**CS0540**](#member-matching-and-resolution): *'interface member' : containing type does not implement interface 'interface'* -- [**CS0541**](#interface-declaration-and-syntax): *'declaration' : explicit interface declaration can only be declared in a class or struct* -- [**CS0550**](#missing-or-incomplete-implementations): *'accessor' adds an accessor not found in interface member 'property'* -- [**CS0551**](#missing-or-incomplete-implementations): *Explicit interface implementation 'implementation' is missing accessor 'accessor'* -- [**CS0630**](#special-implementation-restrictions): *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* -- [**CS0686**](#accessor-implementation-and-conflicts): *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* -- [**CS0736**](#method-visibility-and-modifiers): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* -- [**CS0737**](#method-visibility-and-modifiers): *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* -- [**CS0738**](#return-types-and-signatures): *'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'.* -- [**CS8705**](#ambiguous-and-conflicting-implementations): *Interface member 'member' does not have a most specific implementation. Neither 'implementation1', nor 'implementation2' are most specific.* -- [**CS8854**](#return-types-and-signatures): *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* -- [**CS9333**](#return-types-and-signatures): *Parameter type must match implemented member declaration.* -- [**CS9334**](#return-types-and-signatures): *Return type must match implemented member declaration.* +- [**CS0531**](#interface-declaration-and-syntax): *'{0}': interface members cannot have a definition* +- [**CS0535**](#missing-or-incomplete-implementations): *'{0}' does not implement interface member '{1}'* +- [**CS0538**](#interface-declaration-and-syntax): *'{0}' in explicit interface declaration is not an interface* +- [**CS0539**](#member-matching-and-resolution): *'{0}' in explicit interface declaration is not found among members of the interface that can be implemented* +- [**CS0540**](#member-matching-and-resolution): *'{0}': containing type does not implement interface '{1}'* +- [**CS0541**](#interface-declaration-and-syntax): *'{0}': explicit interface declaration can only be declared in a class, record, struct or interface* +- [**CS0550**](#missing-or-incomplete-implementations): *'{0}' adds an accessor not found in interface member '{1}'* +- [**CS0551**](#missing-or-incomplete-implementations): *Explicit interface implementation 'implementation' is missing accessor 'accessor'.* +- [**CS0630**](#special-implementation-restrictions): *'{0}' cannot implement interface member '{1}' in type '{2}' because it has an __arglist parameter* +- [**CS0686**](#accessor-implementation-and-conflicts): *Accessor '{0}' cannot implement interface member '{1}' for type '{2}'. Use an explicit interface implementation.* +- [**CS0736**](#method-visibility-and-modifiers): *'{0}' does not implement instance interface member '{1}'. '{2}' cannot implement the interface member because it is static.* +- [**CS0737**](#method-visibility-and-modifiers): *'{0}' does not implement interface member '{1}'. '{2}' cannot implement an interface member because it is not public.* +- [**CS0738**](#return-types-and-signatures): *'{0}' does not implement interface member '{1}'. '{2}' cannot implement '{1}' because it does not have the matching return type of '{3}'.* +- [**CS8705**](#ambiguous-and-conflicting-implementations): *Interface member '{0}' does not have a most specific implementation. Neither '{1}', nor '{2}' are most specific.* +- [**CS8854**](#return-types-and-signatures): *'{0}' does not implement interface member '{1}'. '{2}' cannot implement '{1}'.* +- [**CS9333**](#return-types-and-signatures): *'{0}': type must be '{1}' to match implemented member '{2}'* +- [**CS9334**](#return-types-and-signatures): *'{0}': return type must be '{1}' to match implemented member '{2}'* ## Interface declaration and syntax @@ -92,9 +92,9 @@ The following errors relate to proper syntax and structure when declaring explic - **CS0071**: *An explicit interface implementation of an event must use event accessor syntax* - **CS0106**: *The modifier 'modifier' is not valid for this item* -- **CS0531**: *'member' : interface members cannot have a definition* +- **CS0531**: *'{0}': interface members cannot have a definition* - **CS0538**: *'name' in explicit interface declaration is not an interface* -- **CS0541**: *'declaration' : explicit interface declaration can only be declared in a class or struct* +- **CS0541**: *'{0}': explicit interface declaration can only be declared in a class, record, struct or interface* You can correct these errors using the following techniques: @@ -111,7 +111,7 @@ For more information, see [Interfaces](../../fundamentals/types/interfaces.md), The following errors occur when a class fails to fully implement an interface or implements members that don't match the interface contract: -- **CS0535**: *'class' does not implement interface member 'member'* +- **CS0535**: *'{0}' does not implement interface member '{1}'* - **CS0550**: *'accessor' adds an accessor not found in interface member 'property'* - **CS0551**: *Explicit interface implementation 'implementation' is missing accessor 'accessor'* @@ -143,8 +143,8 @@ For more information, see [Interfaces](../../fundamentals/types/interfaces.md), 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 a member of interface* -- **CS0540**: *'interface member' : containing type does not implement interface 'interface'* +- **CS0539**: *'{0}' in explicit interface declaration is not found among members of the interface that can be implemented* +- **CS0540**: *'{0}': containing type does not implement interface '{1}'* You can correct these errors using the following techniques: @@ -173,8 +173,8 @@ For more information, see [Constraints on type parameters](../../programming-gui The following errors occur when implementing interface methods with incorrect accessibility or modifiers: -- **CS0736**: *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static.* -- **CS0737**: *'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public.* +- **CS0736**: *'{0}' does not implement instance interface member '{1}'. '{2}' cannot implement the interface member because it is static.* +- **CS0737**: *'{0}' does not implement interface member '{1}'. '{2}' cannot implement an interface member because it is not public.* You can correct these errors using the following techniques: From f1d3f7c9a61bc11cef6997d37693a00f0ed993dc Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 15:55:38 -0500 Subject: [PATCH 09/11] final edit pass --- .github/prompts/error-consolidation.md | 2 +- ....md => interface-implementation-errors.md} | 199 +++++++----------- docs/csharp/language-reference/toc.yml | 6 +- .../explicit-interface-implementation.md | 2 +- 4 files changed, 84 insertions(+), 125 deletions(-) rename docs/csharp/language-reference/compiler-messages/{explicit-interface-errors.md => interface-implementation-errors.md} (65%) diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 4e94dec8d9845..43a9e9da2f695 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -76,4 +76,4 @@ For every line in this table, verify that the error message associated with this -Now, ensure every error instance of the same message matches exactly. +Now, ensure every instance of an error message matches the text in the highlighted table. \ No newline at end of file diff --git a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md b/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md similarity index 65% rename from docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md rename to docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md index aa5dd81604468..ebf4b9e7df3e4 100644 --- a/docs/csharp/language-reference/compiler-messages/explicit-interface-errors.md +++ b/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md @@ -1,6 +1,6 @@ --- -title: Resolve errors and warnings that explicit interface implementation. -description: These compiler errors and warnings indicate errors in declaring methods that explicitly implement an interface member. +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" @@ -54,97 +54,98 @@ helpviewer_keywords: ms.date: 11/12/2025 ai-usage: ai-assisted --- -# Resolve errors and warnings related to attribute declarations or attribute use in your code +# 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 '{0}' is not valid for this item* -- [**CS0277**](#accessor-implementation-and-conflicts): *'{0}' does not implement interface member '{1}'. '{2}' is not public.* -- [**CS0425**](#generic-type-constraints): *The constraints for type parameter '{0}' of method '{1}' must match the constraints for type parameter '{2}' of interface method '{3}'. Consider using an explicit interface implementation instead.* +- [**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 '{0}' cannot implement interface accessor '{1}' for type '{2}'. Use an explicit interface implementation.* +- [**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): *'{0}': interface members cannot have a definition* -- [**CS0535**](#missing-or-incomplete-implementations): *'{0}' does not implement interface member '{1}'* -- [**CS0538**](#interface-declaration-and-syntax): *'{0}' in explicit interface declaration is not an interface* -- [**CS0539**](#member-matching-and-resolution): *'{0}' in explicit interface declaration is not found among members of the interface that can be implemented* -- [**CS0540**](#member-matching-and-resolution): *'{0}': containing type does not implement interface '{1}'* -- [**CS0541**](#interface-declaration-and-syntax): *'{0}': explicit interface declaration can only be declared in a class, record, struct or interface* -- [**CS0550**](#missing-or-incomplete-implementations): *'{0}' adds an accessor not found in interface member '{1}'* -- [**CS0551**](#missing-or-incomplete-implementations): *Explicit interface implementation 'implementation' is missing accessor 'accessor'.* -- [**CS0630**](#special-implementation-restrictions): *'{0}' cannot implement interface member '{1}' in type '{2}' because it has an __arglist parameter* -- [**CS0686**](#accessor-implementation-and-conflicts): *Accessor '{0}' cannot implement interface member '{1}' for type '{2}'. Use an explicit interface implementation.* -- [**CS0736**](#method-visibility-and-modifiers): *'{0}' does not implement instance interface member '{1}'. '{2}' cannot implement the interface member because it is static.* -- [**CS0737**](#method-visibility-and-modifiers): *'{0}' does not implement interface member '{1}'. '{2}' cannot implement an interface member because it is not public.* -- [**CS0738**](#return-types-and-signatures): *'{0}' does not implement interface member '{1}'. '{2}' cannot implement '{1}' because it does not have the matching return type of '{3}'.* -- [**CS8705**](#ambiguous-and-conflicting-implementations): *Interface member '{0}' does not have a most specific implementation. Neither '{1}', nor '{2}' are most specific.* -- [**CS8854**](#return-types-and-signatures): *'{0}' does not implement interface member '{1}'. '{2}' cannot implement '{1}'.* -- [**CS9333**](#return-types-and-signatures): *'{0}': type must be '{1}' to match implemented member '{2}'* -- [**CS9334**](#return-types-and-signatures): *'{0}': return type must be '{1}' to match implemented member '{2}'* +- [**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 'modifier' is not valid for this item* -- **CS0531**: *'{0}': interface members cannot have a definition* -- **CS0538**: *'name' in explicit interface declaration is not an interface* -- **CS0541**: *'{0}': explicit interface declaration can only be declared in a class, record, struct or interface* +- **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: - 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 cannot be marked as abstract because they cannot 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**). Prior to C# 8.0, interface members cannot contain implementations; starting with C# 8.0, you can provide [default interface methods](../../whats-new/csharp-8.md#default-interface-methods) using specific syntax. +- 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 cannot be declared at the namespace level or in other contexts. +- 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). -## Missing or incomplete implementations +## Return types and signatures -The following errors occur when a class fails to fully implement an interface or implements members that don't match the interface contract: +The following errors occur when the implementing method's signature doesn't match the interface member declaration: -- **CS0535**: *'{0}' does not implement interface member '{1}'* -- **CS0550**: *'accessor' adds an accessor not found in interface member 'property'* -- **CS0551**: *Explicit interface implementation 'implementation' is missing accessor 'accessor'* +- **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: -- Provide an implementation for every member declared in the interface, or declare the type as `abstract` (**CS0535**). When a class or struct declares that it implements an interface, it establishes a contract to provide concrete implementations for all interface members, so 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. +- 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) and [Properties](../../programming-guide/classes-and-structs/properties.md). +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Init-only setters](../keywords/init.md). -## Accessor implementation and conflicts +## Missing or incomplete implementations -The following errors occur when implementing interface properties or events with accessor methods that have visibility issues or naming conflicts: +The following errors occur when a class fails to fully implement an interface or implements members that don't match the interface contract: -- **CS0277**: *'class' does not implement interface member 'accessor'. 'class accessor' is not public* -- **CS0470**: *Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation.* -- **CS0686**: *Accessor 'accessor' cannot implement interface member 'member' for type 'type'. Use an explicit interface implementation.* +- **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: -- Remove any access modifiers from property accessors that restrict visibility below `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. +- 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), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Events](../../programming-guide/events/index.md). +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**: *'{0}' in explicit interface declaration is not found among members of the interface that can be implemented* -- **CS0540**: *'{0}': containing type does not implement interface '{1}'* +- **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: @@ -157,12 +158,12 @@ For more information, see [Interfaces](../../fundamentals/types/interfaces.md) a The following errors occur when implementing generic interface methods with type parameter constraints: -- **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.* -- **CS0460**: *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly* +- **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 because the constraints are part of the method signature contract, although the constraints don't have to be literally identical as long as they express the same set of requirements. +- 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. @@ -173,53 +174,53 @@ For more information, see [Constraints on type parameters](../../programming-gui The following errors occur when implementing interface methods with incorrect accessibility or modifiers: -- **CS0736**: *'{0}' does not implement instance interface member '{1}'. '{2}' cannot implement the interface member because it is static.* -- **CS0737**: *'{0}' does not implement interface member '{1}'. '{2}' cannot implement an interface member because it is not public.* +- **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**). Interface members are instance members by design and must be invoked on an instance of the implementing type, so static methods cannot fulfill the interface contract because they belong to the type itself rather than to instances of the type. +- 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). -## Ambiguous and conflicting implementations +## Accessor implementation and conflicts -The following errors occur when the compiler cannot determine which interface implementation to use: +The following errors occur when implementing interface properties or events with accessor methods that have visibility issues or naming conflicts: -- **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 'implementation1', nor 'implementation2' are most specific.* +- **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: -- 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 cannot 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, and 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. +- Remove any access modifiers from property accessors that restrict visibility 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) and [Default Interface Methods](../../whats-new/csharp-8.md#default-interface-methods). +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Events](../../programming-guide/events/index.md). -## Return types and signatures +## Ambiguous and conflicting implementations -The following errors occur when the implementing method's signature doesn't match the interface member declaration: +The following errors occur when the compiler can't determine which interface implementation to use: -- **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'.* -- **CS8854**: *'type' does not implement interface member 'member'. 'implementation' cannot implement 'member' because it does not have the matching 'init' setter of 'property'.* -- **CS9333**: *Parameter type must match implemented member declaration.* -- **CS9334**: *Return type must match implemented member declaration.* +- **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.* +interface.* 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. +- 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), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Init-only setters](../../whats-new/csharp-9.md#init-only-setters). +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**: *'method' cannot implement interface member 'member' in type 'type' because it has an __arglist parameter* +- **CS0630**: *Member cannot implement interface member because it has an __arglist parameter.* You can correct this error using the following techniques: @@ -227,45 +228,3 @@ You can correct this error using the following techniques: - 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 6c6dfcc1c13a3..74f177bc16a86 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -515,13 +515,13 @@ items: async, await, CS1983, CS1985, CS1986, CS1989, CS1991, CS1992, CS1994, CS1995, CS1996, CS1997, CS1998, CS4008, CS4009, CS4014, CS4032, CS4033, CS8892, CS9123, CS9330 - - name: Explicit interface implementation - href: ./compiler-messages/explicit-interface-errors.md + - 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 + CS9333, CS9334 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > 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: From 76b64f136693b3586575b7de071626a262e2455f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 16:45:49 -0500 Subject: [PATCH 10/11] review feedback. --- .openpublishing.redirection.csharp.json | 38 +++++++++---------- .../interface-implementation-errors.md | 7 ++-- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index a081986001e25..ca95ddccddc1a 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -317,15 +317,15 @@ }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0071.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0106.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0277.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0178.md", @@ -369,19 +369,19 @@ }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0686.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0736.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0737.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0738.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0767.md", @@ -1863,11 +1863,11 @@ }, { "source_path_from_root": "/docs/csharp/misc/cs0425.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0460.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0466.md", @@ -1875,19 +1875,19 @@ }, { "source_path_from_root": "/docs/csharp/misc/cs0470.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0473.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0531.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0535.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0501.md", @@ -1919,27 +1919,27 @@ }, { "source_path_from_root": "/docs/csharp/misc/cs0538.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0539.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0540.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0541.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0550.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0551.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/explicit-interface-errors" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.md" }, { "source_path_from_root": "/docs/csharp/misc/cs0553.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 index ebf4b9e7df3e4..aad25e16b3e2e 100644 --- a/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md +++ b/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md @@ -98,7 +98,7 @@ The following errors relate to proper syntax and structure when declaring explic You can correct these errors using the following techniques: -- 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. +- 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. @@ -194,7 +194,7 @@ The following errors occur when implementing interface properties or events with You can correct these errors using the following techniques: -- Remove any access modifiers from property accessors that restrict visibility 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. +- 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. @@ -206,12 +206,11 @@ The following errors occur when the compiler can't determine which interface imp - **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.* -interface.* 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. +- 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). From 502eaf800c4fa0a96672abfe4041bed110de605d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 Nov 2025 16:50:00 -0500 Subject: [PATCH 11/11] build issue --- .openpublishing.redirection.csharp.json | 38 ++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index ca95ddccddc1a..c773c4e821a7b 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -317,15 +317,15 @@ }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0071.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.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.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.md" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0178.md", @@ -369,19 +369,19 @@ }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0686.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.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.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.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.md" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0767.md", @@ -1863,11 +1863,11 @@ }, { "source_path_from_root": "/docs/csharp/misc/cs0425.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.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.md" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" }, { "source_path_from_root": "/docs/csharp/misc/cs0466.md", @@ -1875,19 +1875,19 @@ }, { "source_path_from_root": "/docs/csharp/misc/cs0470.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.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.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.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.md" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" }, { "source_path_from_root": "/docs/csharp/misc/cs0501.md", @@ -1919,27 +1919,27 @@ }, { "source_path_from_root": "/docs/csharp/misc/cs0538.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors.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.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.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.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.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.md" + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" }, { "source_path_from_root": "/docs/csharp/misc/cs0553.md",