diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index c96c2f381d53d..43a9e9da2f695 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. @@ -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 instance of an error message matches the text in the highlighted table. \ No newline at end of file diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index a8867a28f0d8f..c773c4e821a7b 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -315,6 +315,18 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0034.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0071.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0106.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0277.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0178.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -355,6 +367,22 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0650.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0686.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0736.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0737.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0738.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0767.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -1833,10 +1861,34 @@ "source_path_from_root": "/docs/csharp/misc/cs0457.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0425.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0460.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0466.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0470.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0473.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0531.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0535.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0501.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-declarations" @@ -1865,6 +1917,30 @@ "source_path_from_root": "/docs/csharp/misc/cs0526.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/constructor-errors#constructor-declarations" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0538.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0539.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0540.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0541.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0550.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0551.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/interface-implementation-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0553.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" diff --git a/docs/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/interface-implementation-errors.md b/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md new file mode 100644 index 0000000000000..aad25e16b3e2e --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/interface-implementation-errors.md @@ -0,0 +1,229 @@ +--- +title: Resolve errors and warnings related to interface implementation. +description: These compiler errors and warnings indicate errors in declaring methods that implement an interface member. +f1_keywords: + - "CS0071" + - "CS0106" + - "CS0277" + - "CS0425" + - "CS0460" + - "CS0470" + - "CS0473" + - "CS0531" + - "CS0535" + - "CS0538" + - "CS0539" + - "CS0540" + - "CS0541" + - "CS0550" + - "CS0551" + - "CS0630" + - "CS0686" + - "CS0736" + - "CS0737" + - "CS0738" + - "CS8705" + - "CS8854" + - "CS9333" + - "CS9334" +helpviewer_keywords: + - "CS0071" + - "CS0106" + - "CS0277" + - "CS0425" + - "CS0460" + - "CS0470" + - "CS0473" + - "CS0531" + - "CS0535" + - "CS0538" + - "CS0539" + - "CS0540" + - "CS0541" + - "CS0550" + - "CS0551" + - "CS0630" + - "CS0686" + - "CS0736" + - "CS0737" + - "CS0738" + - "CS8705" + - "CS8854" + - "CS9333" + - "CS9334" +ms.date: 11/12/2025 +ai-usage: ai-assisted +--- +# Resolve errors and warnings related to members that implement an interface + +This article covers the following compiler errors: + + +- [**CS0071**](#interface-declaration-and-syntax): *An explicit interface implementation of an event must use event accessor syntax.* +- [**CS0106**](#interface-declaration-and-syntax): *The modifier is not valid for this item.* +- [**CS0277**](#accessor-implementation-and-conflicts): *Member does not implement interface member because it is not public.* +- [**CS0425**](#generic-type-constraints): *The constraints for type parameter of method must match the constraints for type parameter of interface method. Consider using an explicit interface implementation instead.* +- [**CS0460**](#generic-type-constraints): *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly, except for either a 'class', or a 'struct' constraint.* +- [**CS0470**](#accessor-implementation-and-conflicts): *Method cannot implement interface accessor for type. Use an explicit interface implementation.* +- [**CS0473**](#ambiguous-and-conflicting-implementations): *Explicit interface implementation 'method name' matches more than one interface member. Which interface member is actually chosen is implementation-dependent. Consider using a non-explicit implementation instead.* +- [**CS0531**](#interface-declaration-and-syntax): *Interface members cannot have a definition.* +- [**CS0535**](#missing-or-incomplete-implementations): *Member does not implement interface member.* +- [**CS0538**](#interface-declaration-and-syntax): *Member in explicit interface declaration is not an interface.* +- [**CS0539**](#member-matching-and-resolution): *Member in explicit interface declaration is not found among members of the interface that can be implemented*. +- [**CS0540**](#member-matching-and-resolution): *Containing type does not implement interface member.* +- [**CS0541**](#interface-declaration-and-syntax): *Explicit interface declaration can only be declared in a class, record, struct or interface.* +- [**CS0550**](#missing-or-incomplete-implementations): *Member adds an accessor not found in interface member.* +- [**CS0551**](#missing-or-incomplete-implementations): *Explicit interface implementation is missing an accessor.* +- [**CS0630**](#special-implementation-restrictions): *Member cannot implement interface member because it has an __arglist parameter.* +- [**CS0686**](#accessor-implementation-and-conflicts): *Accessor cannot implement interface member. Use an explicit interface implementation.* +- [**CS0736**](#method-visibility-and-modifiers): *Member does not implement instance interface member. It cannot implement the interface member because it is static.* +- [**CS0737**](#method-visibility-and-modifiers): *Member does not implement interface member. It cannot implement an interface member because it is not public.* +- [**CS0738**](#return-types-and-signatures): *Member does not implement interface member. It cannot because it does not have the matching return type.* +- [**CS8705**](#ambiguous-and-conflicting-implementations): *Interface member does not have a most specific implementation. Neither member is most specific.* +- [**CS8854**](#return-types-and-signatures): *Member does not implement interface member.* +- [**CS9333**](#return-types-and-signatures): *Parameter type must match implemented member.* +- [**CS9334**](#return-types-and-signatures): *Return type must match implemented member.* + +## Interface declaration and syntax + +The following errors relate to proper syntax and structure when declaring explicit interface implementations: + +- **CS0071**: *An explicit interface implementation of an event must use event accessor syntax.* +- **CS0106**: *The modifier is not valid for this item.* +- **CS0531**: *Interface members cannot have a definition.* +- **CS0538**: *Member in explicit interface declaration is not an interface.* +- **CS0541**: *Explicit interface declaration can only be declared in a class, record, struct or interface.* + +You can correct these errors using the following techniques: + +- You must manually provide `add` and `remove` event accessors when explicitly implementing an interface event (**CS0071**). The compiler doesn't automatically generate these accessors for explicit interface implementations, so you must define them explicitly to specify how the event is stored and managed. +- Remove the `public` modifier from explicit interface implementations (**CS0106**). Explicit interface implementations are implicitly public when accessed through the interface type, making the `public` keyword redundant and not allowed in this context. +- Remove the `abstract` modifier from explicit interface implementations (**CS0106**). Explicit interface implementations provide the actual implementation and can't be marked as abstract because they can't be overridden in derived classes. +- Remove the method body from interface member declarations, or move the implementation to a class or struct that implements the interface (**CS0531**). Before C# 8.0, interface members can't contain implementations; starting with C# 8.0, you can provide [default interface methods](../keywords/interface.md#default-interface-members) using specific syntax. +- Verify that the type specified in the explicit interface declaration is an actual interface type (**CS0538**). Only interface types can be used in explicit interface implementation syntax; attempting to use a class or other non-interface type violates the explicit implementation rules. +- Move explicit interface declarations into a class or struct that declares the interface in its base list (**CS0541**). Explicit interface implementations must appear within the body of a class or struct type and can't be declared at the namespace level or in other contexts. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md), and [How to implement interface events](../../programming-guide/events/how-to-implement-interface-events.md). + +## Return types and signatures + +The following errors occur when the implementing method's signature doesn't match the interface member declaration: + +- **CS0738**: *Member does not implement interface member. It cannot because it does not have the matching return type.* +- **CS8854**: *Member does not implement interface member.* +- **CS9333**: *Parameter type must match implemented member.* +- **CS9334**: *Return type must match implemented member.* + +You can correct these errors using the following techniques: + +- Change the return type of the implementing method to exactly match the return type declared in the interface member (**CS0738**, **CS9334**). The signature of the implementation must match the interface declaration precisely because the method signature is part of the contract that determines which interface member is being implemented. +- Ensure that parameter types in the implementing method exactly match the parameter types declared in the interface member (**CS9333**). Each parameter must have the identical type in the same position as specified in the interface declaration, as parameter types are fundamental components of the method signature that the compiler uses to match implementations to interface members. +- Add an `init` accessor to the implementing property when the interface property declares an `init` setter (**CS8854**). The `init` keyword allows property initialization during object construction while preventing modification afterward, and the implementing property must provide this same initialization-only behavior to satisfy the interface contract. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Init-only setters](../keywords/init.md). + +## Missing or incomplete implementations + +The following errors occur when a class fails to fully implement an interface or implements members that don't match the interface contract: + +- **CS0535**: *Member does not implement interface member.* +- **CS0550**: *Member adds an accessor not found in interface member.* +- **CS0551**: *Explicit interface implementation is missing an accessor.* + +You can correct these errors using the following techniques: + +- Provide an implementation for every member declared in the interface, or declare the type as `abstract` (**CS0535**). Each member must be implemented to satisfy the interface requirements. +- Remove any accessors from the implementing property that aren't declared in the interface property (**CS0550**). The implementing property can only include the accessors explicitly declared in the interface definition, ensuring that the implementation doesn't add functionality beyond what the interface contract specifies. +- Add all required accessors to the explicit interface implementation to match the interface declaration (**CS0551**). Each accessor declared in the interface must have a corresponding accessor in the implementation with matching signatures, as the implementation must fulfill the complete accessor contract defined by the interface. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Properties](../../programming-guide/classes-and-structs/properties.md). + +## Member matching and resolution + +The following errors occur when attempting to implement interface members that don't exist in the interface or when the containing type doesn't declare the interface: + +- **CS0539**: *Member in explicit interface declaration is not found among members of the interface that can be implemented*. +- **CS0540**: *Containing type does not implement interface member.* + +You can correct these errors using the following techniques: + +- Verify that the member name and signature in the explicit interface implementation exactly match a member declared in the interface, or remove the incorrect implementation (**CS0539**). The member you're attempting to implement must actually exist in the interface definition with matching name, return type, and parameter types, as explicit interface implementation requires precise correspondence with the interface contract. +- Add the interface to the class's or struct's base list, or remove the explicit interface implementation (**CS0540**). A type can only explicitly implement members of interfaces that it declares in its inheritance list, so the implementing type must establish the interface relationship before it can provide explicit implementations. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md). + +## Generic type constraints + +The following errors occur when implementing generic interface methods with type parameter constraints: + +- **CS0425**: *The constraints for type parameter of method must match the constraints for type parameter of interface method. Consider using an explicit interface implementation instead.* +- **CS0460**: *Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly, except for either a 'class', or a 'struct' constraint.* + +You can correct these errors using the following techniques: + +- Ensure the `where` clause in the implementing method is identical to the interface method declaration, or matches the semantic meaning of the constraints (**CS0425**). The type parameter constraints in the implementation must match those defined in the interface or base method. +- Remove explicit constraint declarations from override and explicit interface implementation methods (**CS0460**). The override method inherits its constraints automatically from the base or interface method, so redeclaring them is redundant and not permitted except for specific cases allowed in C# 8 and later. +- Apply the `default` constraint to resolve ambiguities with nullable reference types in override and explicit interface implementations when using C# 9 or later (**CS0460**). This exception to the constraint inheritance rule allows you to explicitly specify the default constraint to disambiguate nullable annotation contexts. +- Explicitly specify `where T : class` or `where T : struct` constraints on override and explicit interface implementation methods when using C# 8 or later to enable nullable reference type annotations (**CS0460**). These specific constraints are permitted to support nullable reference type analysis on type parameters that are constrained to reference or value types. + +For more information, see [Constraints on type parameters](../../programming-guide/generics/constraints-on-type-parameters.md), [Interfaces](../../fundamentals/types/interfaces.md), and [Nullable reference types](../../nullable-references.md). + +## Method visibility and modifiers + +The following errors occur when implementing interface methods with incorrect accessibility or modifiers: + +- **CS0736**: *Member does not implement instance interface member. It cannot implement the interface member because it is static.* +- **CS0737**: *Member does not implement interface member. It cannot implement an interface member because it is not public.* + +You can correct these errors using the following techniques: + +- Remove the `static` modifier from the method declaration that implements the interface member (**CS0736**). Before C# 10, interface members are instance members, not static members. +- Add the `public` access modifier to the method that implements the interface member (**CS0737**). All interface members are implicitly `public` because interfaces define a contract for public behavior, so the implementing method must also have public accessibility to be accessible through the interface reference. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md). + +## Accessor implementation and conflicts + +The following errors occur when implementing interface properties or events with accessor methods that have visibility issues or naming conflicts: + +- **CS0277**: *Member does not implement interface member because it is not public.* +- **CS0470**: *Method cannot implement interface accessor for type. Use an explicit interface implementation.* +- **CS0686**: *Accessor cannot implement interface member. Use an explicit interface implementation.* + +You can correct these errors using the following techniques: + +- Remove any access modifiers from property accessors that restrict visibility to less visible than `public`, or add the `public` modifier if it's missing (**CS0277**). All interface members are implicitly `public`, so the implementing accessor must also have public accessibility to satisfy the interface contract and be accessible through the interface type. +- Replace methods with accessor-like names (such as `get_PropertyName`) with proper property syntax using explicit interface implementation (**CS0470**). The compiler generates accessor methods internally for properties, and attempting to manually create methods with these reserved names conflicts with the property implementation mechanism. +- Use explicit interface implementation syntax to resolve naming conflicts when the interface contains method names that match the auto-generated accessor methods (**CS0686**). The compiler automatically generates methods like `get_Property` and `set_Property` for properties, and `add_Event` and `remove_Event` for events, so if an interface declares methods with these exact names, explicit implementation is required to disambiguate between the interface method and the compiler-generated accessor. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md), [Properties](../../programming-guide/classes-and-structs/properties.md), and [Events](../../programming-guide/events/index.md). + +## Ambiguous and conflicting implementations + +The following errors occur when the compiler can't determine which interface implementation to use: + +- **CS0473**: *Explicit interface implementation 'method name' matches more than one interface member. Which interface member is actually chosen is implementation-dependent. Consider using a non-explicit implementation instead.* +- **CS8705**: *Interface member 'member' does not have a most specific implementation. Neither is most specific.* + +You can correct these errors using the following techniques: + +- Eliminate the explicit interface implementation and instead use a single implicit public implementation for both interface methods (**CS0473**). When a generic method acquires the same signature as a non-generic method (such as when implementing `ITest` where both `TestMethod(int)` and `TestMethod(T)` become identical), the common language infrastructure metadata system can't unambiguously determine which interface member binds to which implementation slot, so using implicit implementation allows the single method to satisfy both interface requirements. +- Provide an explicit implementation in the implementing class or struct that resolves the ambiguity between multiple default implementations (**CS8705**). This error typically occurs with diamond inheritance patterns where a class implements multiple interfaces that each provide default implementations for the same member. The compiler needs you to explicitly specify which implementation to use, or provide your own implementation. +- Restructure the interface hierarchy to avoid diamond inheritance conflicts where multiple interfaces provide default implementations for the same member (**CS8705**). By redesigning the interface relationships or consolidating the default implementations into a single interface, you can eliminate the ambiguity that prevents the compiler from determining the most specific implementation. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [Default Interface Methods](../keywords/interface.md#default-interface-members). + +## Special implementation restrictions + +The following error occurs when using special parameter types that aren't compatible with interface implementation: + +- **CS0630**: *Member cannot implement interface member because it has an __arglist parameter.* + +You can correct this error using the following techniques: + +- Remove the `__arglist` parameter from the implementing method (**CS0630**). The `__arglist` keyword allows methods to accept variable numbers of arguments in an unmanaged way, but this feature is incompatible with interface implementation because interface contracts require predictable, type-safe signatures that can be verified at compile time. +- Replace the `__arglist` parameter with a `params` array parameter for variable-length argument lists (**CS0630**). Unlike `__arglist`, the `params` keyword provides a type-safe mechanism for accepting variable numbers of arguments that is fully compatible with interface implementation and maintains the compile-time type safety that interfaces require. + +For more information, see [Interfaces](../../fundamentals/types/interfaces.md) and [params keyword](../keywords/method-parameters.md#params-modifier). diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index ac5e564fed106..74f177bc16a86 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -515,6 +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: Interface implementation + href: ./compiler-messages/interface-implementation-errors.md + displayName: > + interface, + CS0071, CS0106, CS0277, CS0425, CS0460, CS0470, CS0473, CS0531, CS0535, CS0538, CS0539, CS0540, + CS0541, CS0550, CS0551, CS0630, CS0686, CS0736, CS0737, CS0738, CS8705, CS8707, CS8711, CS8854, + CS9333, CS9334 - name: Reference parameters href: ./compiler-messages/ref-modifiers-errors.md displayName: > @@ -752,8 +759,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 @@ -784,8 +789,6 @@ items: href: ./compiler-messages/cs0103.md - name: CS0104 href: ../misc/cs0104.md - - name: CS0106 - href: ./compiler-messages/cs0106.md - name: CS0107 href: ../misc/cs0107.md - name: CS0110 @@ -956,8 +959,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 @@ -1014,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 @@ -1056,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 @@ -1068,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 @@ -1106,22 +1101,16 @@ items: href: ../misc/cs0528.md - name: CS0529 href: ../misc/cs0529.md - - name: CS0531 - href: ../misc/cs0531.md - name: CS0533 href: ../misc/cs0533.md - name: CS0534 href: ../misc/cs0534.md - - name: CS0535 - href: ../misc/cs0535.md - name: CS0537 href: ../misc/cs0537.md - name: CS0538 href: ../misc/cs0538.md - name: CS0539 href: ../misc/cs0539.md - - name: CS0540 - href: ../misc/cs0540.md - name: CS0541 href: ../misc/cs0541.md - name: CS0542 @@ -1140,8 +1129,6 @@ items: href: ../misc/cs0548.md - name: CS0549 href: ../misc/cs0549.md - - name: CS0550 - href: ../misc/cs0550.md - name: CS0551 href: ../misc/cs0551.md - name: CS0569 @@ -1232,8 +1219,6 @@ items: href: ../misc/cs0682.md - name: CS0683 href: ../misc/cs0683.md - - name: CS0686 - href: ./compiler-messages/cs0686.md - name: CS0689 href: ../misc/cs0689.md - name: CS0690 @@ -1302,12 +1287,6 @@ items: href: ../misc/cs0734.md - name: CS0735 href: ../misc/cs0735.md - - name: CS0736 - href: ../misc/cs0736.md - - name: CS0737 - href: ../misc/cs0737.md - - name: CS0738 - href: ../misc/cs0738.md - name: CS0739 href: ../misc/cs0739.md - name: CS0742 diff --git a/docs/csharp/misc/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/cs0425.md b/docs/csharp/misc/cs0425.md deleted file mode 100644 index c61a74ff81f69..0000000000000 --- a/docs/csharp/misc/cs0425.md +++ /dev/null @@ -1,78 +0,0 @@ ---- -description: "Compiler Error CS0425" -title: "Compiler Error CS0425" -ms.date: 07/20/2015 -f1_keywords: - - "CS0425" -helpviewer_keywords: - - "CS0425" -ms.assetid: cec0391c-a641-43bc-8557-92b23f6ca685 ---- -# Compiler Error CS0425 - -The constraints for type parameter 'type parameter' of method 'method' must match the constraints for type parameter 'type parameter' of interface method 'method'. Consider using an explicit interface implementation instead. - - This error occurs if a virtual generic method is overridden in a derived class and the constraints on the method in the derived class do not match the constraints on the method in the base class. To avoid this error, make sure the `where` clause is identical in both declarations, or implement the interface explicitly. - -## Example 1 - - The following example generates CS0425: - -```csharp -// CS0425.cs - -class C1 -{ -} - -class C2 -{ -} - -interface IBase -{ - void F(ItemType item) where ItemType : C1; -} - -class Derived : IBase -{ - public void F(ItemType item) where ItemType : C2 // CS0425 - { - } -} - -class CMain -{ - public static void Main() - { - } -} -``` - -## Example 2 - - The constraints do not have to be a literal match, as long as the set of constraints has the same meaning. For example, the following is okay: - -```csharp -// CS0425b.cs - -interface J -{ -} - -interface I -{ - void F(S s, T t) where T: J, J; -} - -class C : I -{ - public void F(int s, X x) where X : J - { - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0460.md b/docs/csharp/misc/cs0460.md deleted file mode 100644 index 93633a953186f..0000000000000 --- a/docs/csharp/misc/cs0460.md +++ /dev/null @@ -1,57 +0,0 @@ ---- -description: "Compiler Error CS0460" -title: "Compiler Error CS0460" -ms.date: 06/03/2025 -ai-usage: ai-generated -f1_keywords: - - "CS0460" -helpviewer_keywords: - - "CS0460" -ms.assetid: 98d39ded-d3f9-4520-b912-892e574c056b ---- -# Compiler Error CS0460 - -Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly - - When a generic method that is part of a derived class overrides a method in the base class, you can't specify constraints on the overridden method. The override method in the derived class inherits its constraints from the method in the base class. - -However, there are exceptions to this rule: - -- Starting with C# 9, you can apply the `default` constraint to `override` and explicit interface implementation methods to resolve ambiguities with nullable reference types. -- Starting with C# 8, you can explicitly specify `where T : class` and `where T : struct` constraints on `override` and explicit interface implementation methods to allow annotations for type parameters constrained to reference types. - -## Example - - The following sample generates CS0460 when attempting to redeclare inherited constraints. - -```csharp -// CS0460.cs -// compile with: /target:library -class BaseClass -{ - BaseClass() { } -} - -interface I -{ - void F1() where T : BaseClass; - void F2() where T : struct; - void F3(); - void F4() where T : struct; -} - -class ExpImpl : I -{ - // CS0460 - cannot redeclare inherited constraint. - void I.F1() where T : BaseClass { } - - // Allowed - explicit constraint for struct. - void I.F2() where T : struct { } - - // Valid since C# 8 - explicit class constraint for nullable annotations. - void I.F4() where T : struct { } - - // Valid since C# 9 - default constraint to resolve ambiguities. - void I.F3() where T : default { } -} -``` diff --git a/docs/csharp/misc/cs0470.md b/docs/csharp/misc/cs0470.md deleted file mode 100644 index e26f2fa76725b..0000000000000 --- a/docs/csharp/misc/cs0470.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS0470" -title: "Compiler Error CS0470" -ms.date: 07/20/2015 -f1_keywords: - - "CS0470" -helpviewer_keywords: - - "CS0470" -ms.assetid: b5a8e820-aa5c-4f69-b5c6-01c6a6bb82d9 ---- -# Compiler Error CS0470 - -Method 'method' cannot implement interface accessor 'accessor' for type 'type'. Use an explicit interface implementation. - - This error is generated when an accessor is trying to implement an interface. Explicit interface implementation must be used. - -## Example - - The following sample generates CS0470. - -```csharp -// CS0470.cs -// compile with: /target:library - -interface I -{ - int P { get; } -} - -class MyClass : I -{ - public int get_P() { return 0; } // CS0470 - public int P2 { get { return 0;} } // OK -} -``` diff --git a/docs/csharp/misc/cs0473.md b/docs/csharp/misc/cs0473.md deleted file mode 100644 index 154f06fe2c8af..0000000000000 --- a/docs/csharp/misc/cs0473.md +++ /dev/null @@ -1,65 +0,0 @@ ---- -description: "Compiler Error CS0473" -title: "Compiler Error CS0473" -ms.date: 08/14/2018 -f1_keywords: - - "CS0473" -helpviewer_keywords: - - "CS0473" -ms.assetid: 58eb141e-7da0-41c8-b868-7cd2a15f07f9 ---- -# Compiler Error CS0473 - -Explicit interface implementation 'method name' matches more than one interface member. Which interface member is actually chosen is implementation-dependent. Consider using a non-explicit implementation instead. - -In some cases a generic method might acquire the same signature as a non-generic method. The problem is that there is no way in the common language infrastructure (CLI) metadata system to unambiguously state which method binds to which slot. It is up to the CLI to make that determination. - -## To correct this error - -To correct the error, eliminate the explicit implementation and implement both of the interface methods in the implicit implementation `public int TestMethod(int)`. - -## Example - -The following code generates CS0473: - -```csharp -public interface ITest -{ - int TestMethod(int i); - int TestMethod(T i); -} - -public class ImplementingClass : ITest -{ - int ITest.TestMethod(int i) // CS0473 - { - return i + 1; - } - - public int TestMethod(int i) - { - return i - 1; - } -} - -class T -{ - static int Main() - { - ImplementingClass a = new ImplementingClass(); - if (a.TestMethod(0) != -1) - return -1; - - ITest i_a = a; - System.Console.WriteLine(i_a.TestMethod(0).ToString()); - if (i_a.TestMethod(0) != 1) - return -1; - - return 0; - } -} -``` - -## See also - -- [Odious ambiguous overloads, part two](/archive/blogs/ericlippert/odious-ambiguous-overloads-part-two) diff --git a/docs/csharp/misc/cs0531.md b/docs/csharp/misc/cs0531.md deleted file mode 100644 index ec8b67d32b02e..0000000000000 --- a/docs/csharp/misc/cs0531.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0531" -title: "Compiler Error CS0531" -ms.date: 07/20/2015 -f1_keywords: - - "CS0531" -helpviewer_keywords: - - "CS0531" -ms.assetid: 54c2a98b-84e3-481a-a934-7cd6dffa7677 ---- -# Compiler Error CS0531 - -'member' : interface members cannot have a definition - - Methods that are declared in an [interface](../language-reference/keywords/interface.md) must be implemented in a class that inherits from it and not in the interface itself. - - The following sample generates CS0531: - -```csharp -// CS0531.cs -namespace x -{ - public interface clx - { - int xclx() // CS0531, cannot define xclx - // Try the following declaration instead: - // int xclx(); - { - return 0; - } - } - - public class cly - { - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0535.md b/docs/csharp/misc/cs0535.md deleted file mode 100644 index 970a934aae631..0000000000000 --- a/docs/csharp/misc/cs0535.md +++ /dev/null @@ -1,55 +0,0 @@ ---- -description: "Compiler Error CS0535" -title: "Compiler Error CS0535" -ms.date: 07/20/2015 -f1_keywords: - - "CS0535" -helpviewer_keywords: - - "CS0535" -ms.assetid: 282ed5d6-acb7-445b-999f-27a973ccc0b5 ---- -# Compiler Error CS0535 - -'class' does not implement interface member 'member' - - A [class](../language-reference/keywords/class.md) derived from an [interface](../language-reference/keywords/interface.md), but the class did not implement one or more of the interface's members. A class must implement all members of interfaces from which it derives or else be declared `abstract`. - -## Example 1 - - The following sample generates CS0535. - -```csharp -// CS0535.cs -public interface A -{ - void F(); -} - -public class B : A {} // CS0535 A::F is not implemented - -// OK -public class C : A { - public void F() {} - public static void Main() {} -} -``` - -## Example 2 - - The following sample generates CS0535. - -```csharp -// CS0535_b.cs -using System; -class C : IDisposable {} // CS0535 - -// OK -class D : IDisposable { - void IDisposable.Dispose() {} - public void Dispose() {} - - static void Main() { - using (D d = new D()) {} - } -} -``` diff --git a/docs/csharp/misc/cs0538.md b/docs/csharp/misc/cs0538.md deleted file mode 100644 index 4a48a08193638..0000000000000 --- a/docs/csharp/misc/cs0538.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0538" -title: "Compiler Error CS0538" -ms.date: 07/20/2015 -f1_keywords: - - "CS0538" -helpviewer_keywords: - - "CS0538" -ms.assetid: 46ac205e-16b0-4637-bd0f-9a755ac19f18 ---- -# Compiler Error CS0538 - -'name' in explicit interface declaration is not an interface - - An attempt was made to explicitly declare an [interface](../language-reference/keywords/interface.md), but an interface was not specified. - - The following sample generates CS0538: - -```csharp -// CS0538.cs -interface MyIFace -{ - void F(); -} - -public class MyClass -{ - public void G() - { - } -} - -class C: MyIFace -{ - void MyIFace.F() - { - } - - void MyClass.G() // CS0538, MyClass not an interface - { - } -} -``` diff --git a/docs/csharp/misc/cs0539.md b/docs/csharp/misc/cs0539.md deleted file mode 100644 index 3d96081280b1d..0000000000000 --- a/docs/csharp/misc/cs0539.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0539" -title: "Compiler Error CS0539" -ms.date: 07/20/2015 -f1_keywords: - - "CS0539" -helpviewer_keywords: - - "CS0539" -ms.assetid: 41b8975c-abd1-4a36-98a4-8efa5fb0502a ---- -# Compiler Error CS0539 - -'member' in explicit interface declaration is not a member of interface - - An attempt was made to explicitly declare an [interface](../language-reference/keywords/interface.md) member that does not exist. You should either delete the declaration or change it so that it refers to a valid interface member. - - The following sample generates CS0539: - -```csharp -// CS0539.cs -namespace x -{ - interface I - { - void m(); - } - - public class clx : I - { - void I.x() // CS0539 - { - } - - public static int Main() - { - return 0; - } - } -} -``` diff --git a/docs/csharp/misc/cs0540.md b/docs/csharp/misc/cs0540.md deleted file mode 100644 index 6f50275f81eb0..0000000000000 --- a/docs/csharp/misc/cs0540.md +++ /dev/null @@ -1,60 +0,0 @@ ---- -description: "Compiler Error CS0540" -title: "Compiler Error CS0540" -ms.date: 07/20/2015 -f1_keywords: - - "CS0540" -helpviewer_keywords: - - "CS0540" -ms.assetid: 2da2cd4a-0ff1-45ea-bb72-ea078bc95dea ---- -# Compiler Error CS0540 - -'interface member' : containing type does not implement interface 'interface' - - You attempted to implement an interface member in a [class](../language-reference/keywords/class.md) that does not derive from the [interface](../language-reference/keywords/interface.md). You should either delete the implementation of the interface member or add the interface to the base-class list of the class. - -## Example 1 - - The following sample generates CS0540. - -```csharp -// CS0540.cs -interface I -{ - void m(); -} - -public class Clx -{ - void I.m() {} // CS0540 -} - -// OK -public class Cly : I -{ - void I.m() {} - public static void Main() {} -} -``` - -## Example 2 - - The following sample generates CS0540. - -```csharp -// CS0540_b.cs -using System; -class C { - void IDisposable.Dispose() {} // CS0540 -} - -class D : IDisposable { - void IDisposable.Dispose() {} - public void Dispose() {} - - static void Main() { - using (D d = new D()) {} - } -} -``` diff --git a/docs/csharp/misc/cs0541.md b/docs/csharp/misc/cs0541.md deleted file mode 100644 index 54616a962bee9..0000000000000 --- a/docs/csharp/misc/cs0541.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -description: "Compiler Error CS0541" -title: "Compiler Error CS0541" -ms.date: 07/20/2015 -f1_keywords: - - "CS0541" -helpviewer_keywords: - - "CS0541" -ms.assetid: ed812c07-24f7-43c6-9a44-553f27f6249d ---- -# Compiler Error CS0541 - -'declaration' : explicit interface declaration can only be declared in a class or struct - - An explicit [interface](../language-reference/keywords/interface.md) declaration was found outside a [class](../language-reference/keywords/class.md) or [struct](../language-reference/builtin-types/struct.md). - - The following sample generates CS0541: - -```csharp -// CS0541.cs -namespace x -{ - interface IFace - { - void F(); - } - - interface IFace2 : IFace - { - void IFace.F(); // CS0541 - } -} -``` diff --git a/docs/csharp/misc/cs0550.md b/docs/csharp/misc/cs0550.md deleted file mode 100644 index 64daba3a2be35..0000000000000 --- a/docs/csharp/misc/cs0550.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -description: "Compiler Error CS0550" -title: "Compiler Error CS0550" -ms.date: 07/20/2015 -f1_keywords: - - "CS0550" -helpviewer_keywords: - - "CS0550" -ms.assetid: 57278c17-443c-40f2-9ebd-853558743564 ---- -# Compiler Error CS0550 - -'accessor' adds an accessor not found in interface member 'property' - - The implementation of a property in a derived class contains an accessor that was not specified in the base interface. - - For more information, see [Using Properties](../programming-guide/classes-and-structs/using-properties.md). - -## Example - - The following sample generates CS0550. - -```csharp -// CS0550.cs -namespace x -{ - interface ii - { - int i - { - get; - // add the following accessor to resolve this CS0550 - // set; - } - } - - public class a : ii - { - int ii.i - { - get - { - return 0; - } - set {} // CS0550 no set in interface - } - - public static void Main() {} - } -} -``` diff --git a/docs/csharp/misc/cs0551.md b/docs/csharp/misc/cs0551.md deleted file mode 100644 index fbfc60a40afcb..0000000000000 --- a/docs/csharp/misc/cs0551.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS0551" -title: "Compiler Error CS0551" -ms.date: 07/20/2015 -f1_keywords: - - "CS0551" -helpviewer_keywords: - - "CS0551" -ms.assetid: fb456ecf-dff3-4e39-b9b3-de23d81dadea ---- -# Compiler Error CS0551 - -Explicit interface implementation 'implementation' is missing accessor 'accessor' - - A class that explicitly implements an interface's property must implement all the accessors that the interface defines. - - For more information, see [Using Properties](../programming-guide/classes-and-structs/using-properties.md). - -## Example - - The following sample generates CS0551. - -```csharp -// CS0551.cs -// compile with: /target:library -interface ii -{ - int i - { - get; - set; - } -} - -public class a : ii -{ - int ii.i { set {} } // CS0551 - - // OK - int ii.i - { - set {} - get { return 0; } - } -} -``` diff --git a/docs/csharp/misc/cs0736.md b/docs/csharp/misc/cs0736.md deleted file mode 100644 index 60b8c2adf84cc..0000000000000 --- a/docs/csharp/misc/cs0736.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -description: "Compiler Error CS0736" -title: "Compiler Error CS0736" -ms.date: 07/20/2015 -f1_keywords: - - "CS0736" -helpviewer_keywords: - - "CS0736" -ms.assetid: 06b14feb-81d5-495f-ab2d-6dc3f5e7216f ---- -# Compiler Error CS0736 - -'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is static. - - This error is generated when a static method is either implicitly or explicitly declared as an implementation of an interface member. - -## To correct this error - -- Remove the [static](../language-reference/keywords/static.md) modifier from the method declaration. - -- Change the name of the interface method. - -- Redefine the containing type so that it does not inherit from the interface. - -## Example - - The following code generates CS0736 because `Program.testMethod` is declared as static: - -```csharp -// cs0736.cs -namespace CS0736 -{ - - interface ITest - { - int testMethod(int x); - } - - class Program : ITest // CS0736 - { - public static int testMethod(int x) { return 0; } - // Try the following line instead. - // public int testMethod(int x) { return 0; } - public static void Main() { } - } -} -``` - -## See also - -- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/cs0737.md b/docs/csharp/misc/cs0737.md deleted file mode 100644 index e2354d4ed4cab..0000000000000 --- a/docs/csharp/misc/cs0737.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -description: "Compiler Error CS0737" -title: "Compiler Error CS0737" -ms.date: 07/20/2015 -f1_keywords: - - "CS0737" -helpviewer_keywords: - - "CS0737" -ms.assetid: d2247770-5546-46f2-a01d-8e2ebfcbb859 ---- -# Compiler Error CS0737 - -'type name' does not implement interface member 'member name'. 'method name' cannot implement an interface member because it is not public. - - A method that implements an interface member must have public accessibility. All interface members are `public`. - -## To correct this error - -1. Add the [public](../language-reference/keywords/public.md) access modifier to the method. - -## Example - - The following code generates CS0737: - -```csharp -// cs0737.cs -interface ITest -{ - // Default access of private with no modifier. - int Return42(); - // Try the following line instead. - // public int Return42(); -} - -struct Struct1 : ITest // CS0737 -{ - int Return42() { return (42); } -} - -public class Test -{ - public static int Main(string[] args) - { - Struct1 s1 = new Struct1(); - - return (1); - } - -} -``` - -## See also - -- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/cs0738.md b/docs/csharp/misc/cs0738.md deleted file mode 100644 index 2083f426ac71b..0000000000000 --- a/docs/csharp/misc/cs0738.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -description: "Compiler Error CS0738" -title: "Compiler Error CS0738" -ms.date: 07/20/2015 -f1_keywords: - - "CS0738" -helpviewer_keywords: - - "CS0738" -ms.assetid: 01ce94ee-2435-4326-befc-2b020c441a4f ---- -# Compiler Error CS0738 - -'type name' does not implement interface member 'member name'. 'method name' cannot implement 'interface member' because it does not have the matching return type of ' type name'. - - The return value of an implementing method in a class must match the return value of the interface member that it implements. - -## To correct this error - -1. Change the return type of the method to match that of the interface member. - -## Example - - The following code generates CS0738 because the class method returns `void` and the interface member of the same name returns `int`: - -```csharp -using System; - -interface ITest -{ - int TestMethod(); -} -public class Test: ITest -{ - public void TestMethod() { } // CS0738 - // Try the following line instead. - // public int TestMethod(); -} -``` - -## See also - -- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md b/docs/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error.md index 7a25597850dd4..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 @@ -7,7 +7,6 @@ f1_keywords: - "CS0224" - "CS0257" - "CS0595" - - "CS0630" - "CS0847" - "CS0856" - "CS0857" @@ -339,9 +338,7 @@ f1_keywords: - "CS8700" - "CS8701" - "CS8702" - - "CS8705" - "CS8707" - - "CS8711" - "CS8712" - "CS8715" - "CS8716" @@ -394,7 +391,6 @@ f1_keywords: - "CS8851" - "CS8852" - "CS8853" - - "CS8854" - "CS8855" - "CS8856" - "CS8857" @@ -538,8 +534,6 @@ f1_keywords: - "CS9096" - "CS9097" # C# 14 errors begin here - - "CS9333" - - "CS9334" - "CS9338" helpviewer_keywords: - "errors [C#], additional information" diff --git a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md index 138e11207db6d..4621e6c217a2b 100644 --- a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md +++ b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md @@ -28,7 +28,7 @@ Explicit implementation is also used to resolve cases where two interfaces each [!code-csharp[NameCollisions](~/samples/snippets/csharp/interfaces/ExplicitImplementation.cs#NameCollision)] -An explicit interface implementation doesn't have an access modifier since it isn't accessible as a member of the type it's defined in. Instead, it's only accessible when called through an instance of the interface. If you specify an access modifier for an explicit interface implementation, you get compiler error [CS0106](../../language-reference/compiler-messages/cs0106.md). For more information, see [`interface` (C# Reference)](../../language-reference/keywords/interface.md). +An explicit interface implementation doesn't have an access modifier since it isn't accessible as a member of the type it's defined in. Instead, it's only accessible when called through an instance of the interface. If you specify an access modifier for an explicit interface implementation, you get compiler error [CS0106](../../language-reference/compiler-messages/interface-implementation-errors.md). For more information, see [`interface` (C# Reference)](../../language-reference/keywords/interface.md). You can define an implementation for members declared in an interface. If a class inherits a method implementation from an interface, that method is only accessible through a reference of the interface type. The inherited member doesn't appear as part of the public interface. The following sample defines a default implementation for an interface method: