diff --git a/.github/prompts/error-consolidation.md b/.github/prompts/error-consolidation.md index 1660f67f1bd7e..7d7b4ea99d553 100644 --- a/.github/prompts/error-consolidation.md +++ b/.github/prompts/error-consolidation.md @@ -1,46 +1,60 @@ # Copilot prompts to consolidate error codes. -We're going to edit this file, string-literal.md, to contain information about all errors and warnings related to string and character literal declarations. I'll write prompts for specific tasks. Don't make any edits yet. In future prompts, the destination for new error and warning content is always this file. +Overall steps: +1. Make a new template, by hand. +1. Add any new errors that should be added here. +1. Consolidate existing errors, as identified by person. +1. Run Copilot search for other existing errors that person may have missed. +1. Search for missing errors. ## Add a single existing file into the new consolidated article. -Start with CS1009.md as the source file. -For each source file: +We're going to work through a series of files consolidating errors and warnings related to declaring overloaded operators. + +The destination for all these edits is the overloaded-operator-errors.md file. It already contains a skeleton for the final output. + +For each source file I specify in this chat, you'll do the following tasks: - Add the contents of the source file to the destination.md file. - - Include the source error code in the YML header for `f1_keywords` and `helpviewer_keywords`. + - Include the source error code in the YML header for f1_keywords and helpviewer_keywords. - Add an entry with an anchor for the error error code and its corresponding error message. - Add the contents of the source file as a new H2 in the destination file. -- Add a redirection for the source file in the file `.openpublishing.redirection.csharp.json`. Make the destination point to destination file. Place the new entry in sorted order based on 'source_path_from_root'. + - Add a redirection for the source file in the file .openpublishing.redirection.csharp.json. Make the destination point to destination file. Place the new entry in sorted order based on 'source_path_from_root'. - Update the TOC file: - Add the error code to the list of display names in the TOC for the destination file, sorted by numeric error code. - Remove the TOC entry for the source file. -- Finally, delete the source markdown file. + - Finally, delete the source markdown file. ## Search for other related articles that may be missed. -Search all files in the docs/csharp/language-reference/compiler-messages and the docs/csharp/misc folder for any other errors and warnings that involve string literals or constants. Give me a list to review for possible additional consolidation. +Search all files in the docs/csharp/language-reference/compiler-messages and the docs/csharp/misc folder for any other errors and warnings that involve operator overloading. Give me a list to review for possible additional consolidation. Don't make any edits until the originating user approves. ## Final search in roslyn source +Let's check undocumented errors and the roslyn source for any missing errors. For every error code listed in "sorry-we-don-t-have-specifics-on-this-csharp-error.md" under the `f1_keywords` front matter, do the following: +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. +Give me a list of all error numbers and corresponding error messages that relate to operator overloading. + To make sure you've found all related errors, we'll check the source. Look in `CSharpResources.resx` for any elements where the `` element is a message related to preprocessor tokens. The symbolic constant for that value is in the `name` attribute on the parent `data` element. Find that value in `ErrorCodes.cs`. It will map to the compiler error code, where the code is "CS" followed by the number as a four digit number. Build a list of any related errors, but don't make any edits yet. -For each new file: +I'll give you error codes one by one. For each, I want you to do the following: -- Add the new error code to the front matter of the consolidated article, for both the `f1_keywords` and `helpview_keywords` table. -- Add the new error code and error message to the table at the top of the destination article. -- Add the new error code to the list of `displaName` elements in the TOC file. +- Add the new error code to the front matter of operator-overloading-errors.md, for both the `f1_keywords` and `helpview_keywords` table. +- Add the new error code and error message to the table at the top of operator-overloading-errors.md. +- Add the new error code to the list of `displayName` elements in the TOC file for operator-overloading-errors.md. - Remove the new error code from the front matter in the file `csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-errors.md` file. Note that no redirections need to be added for these error codes. ## Build consolidated sections -For all remaining work, all edits will be in the `string-literal.md` file. The final format should mirror the structure of the `preprocessor-errors.md` file. Every H2 is a theme, all anchors are for the theme, not an individual error code. +For all remaining work, all edits will be in the `overloaded-operator-errors.md` file. The final format should mirror the structure of the `preprocessor-errors.md` file. Every H2 is a theme, all anchors are for the theme, not an individual error code. To do that, make a new H2 section for the theme. Remove all the H2s for the individual error codes that are part of that theme. Where applicable, the new H2 can include text or examples from the H2s you remove. The new section should include links to language reference articles that discuss the feature or theme. The list of errors at the top of the file should remain in numerical order, so it's easy for readers to scan. Each impacted error code should now have a link to the anchor tag for the new section. Repeat the list in the new section, but without the anchors, as shown in the highlighted text. -Start with the section for all errors related to raw string literals. +Understand these instructions, then suggest a list of themes and the included error codes. I'll approve each theme before you begin editing. diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index a33f7459c71e8..2ef465f8e10f9 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -359,6 +359,14 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0270.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0552.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0563.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0650.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -1549,6 +1557,14 @@ "source_path_from_root": "/docs/csharp/misc/cs0035.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0056.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0057.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0105.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" @@ -1589,6 +1605,22 @@ "source_path_from_root": "/docs/csharp/misc/cs0206.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0215.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0216.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0217.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0218.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0225.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" @@ -1649,6 +1681,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0440.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0448.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0457.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" @@ -1685,6 +1721,74 @@ "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/cs0553.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0554.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0555.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0556.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0557.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0558.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0559.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0562.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0564.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0567.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0590.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0660.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0661.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0715.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1037.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1553.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1554.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overloaded-operator-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs0568.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/constructor-errors#constructors-in-struct-types" diff --git a/docs/csharp/language-reference/compiler-messages/cs0552.md b/docs/csharp/language-reference/compiler-messages/cs0552.md deleted file mode 100644 index 934d730087623..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0552.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -description: "Compiler Error CS0552" -title: "Compiler Error CS0552" -ms.date: 07/20/2015 -f1_keywords: - - "CS0552" -helpviewer_keywords: - - "CS0552" -ms.assetid: ce5cfb26-8406-4ca0-adb7-55d1d03d8145 ---- -# Compiler Error CS0552 - -'conversion routine' : user defined conversion to/from interface - - You cannot create a user-defined conversion to or from an interface. If you need the conversion routine, resolve this error by making the interface a class or derive a class from the interface. - - The following sample generates CS0552: - -```csharp -// CS0552.cs -public interface ii -{ -} - -public class a -{ - // delete the routine to resolve CS0552 - public static implicit operator ii(a aa) // CS0552 - { - return new ii(); - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs0563.md b/docs/csharp/language-reference/compiler-messages/cs0563.md deleted file mode 100644 index a6edbab270f38..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0563.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0563" -title: "Compiler Error CS0563" -ms.date: 07/20/2015 -f1_keywords: - - "CS0563" -helpviewer_keywords: - - "CS0563" -ms.assetid: c1561e4e-7f00-41ff-abff-b8228aee66a4 ---- -# Compiler Error CS0563 - -One of the parameters of a binary operator must be the containing type - -The method declaration for an [operator overload](../operators/operator-overloading.md) must follow certain guidelines. - -## Example - - The following sample generates CS0563: - -```csharp -// CS0563.cs -public class iii -{ - public static implicit operator int(iii x) - { - return 0; - } - public static implicit operator iii(int x) - { - return null; - } - public static int operator +(int aa, int bb) // CS0563 - // Use the following line instead: - // public static int operator +(int aa, iii bb) - { - return 0; - } - public static void Main() - { - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/overloaded-operator-errors.md b/docs/csharp/language-reference/compiler-messages/overloaded-operator-errors.md new file mode 100644 index 0000000000000..b356db406a1d7 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/overloaded-operator-errors.md @@ -0,0 +1,368 @@ +--- +title: Errors and warnings related to user defined operator declarations +description: This article helps you diagnose and correct compiler errors and warnings when you declare user defined operators in your types +f1_keywords: + - "CS0056" + - "CS0057" + - "CS0215" + - "CS0216" + - "CS0217" + - "CS0218" + - "CS0448" + - "CS0552" + - "CS0553" + - "CS0554" + - "CS0555" + - "CS0556" + - "CS0557" + - "CS0558" + - "CS0559" + - "CS0562" + - "CS0563" + - "CS0564" + - "CS0567" + - "CS0590" + - "CS0660" + - "CS0661" + - "CS0715" + - "CS1037" + - "CS1553" + - "CS8930" + - "CS8931" + - "CS9023" + - "CS9024" + - "CS9025" + - "CS9308" + - "CS9310" + - "CS9311" + - "CS9312" + - "CS9313" +helpviewer_keywords: + - "CS0056" + - "CS0057" + - "CS0215" + - "CS0216" + - "CS0217" + - "CS0218" + - "CS0448" + - "CS0552" + - "CS0553" + - "CS0554" + - "CS0555" + - "CS0556" + - "CS0557" + - "CS0558" + - "CS0559" + - "CS0562" + - "CS0563" + - "CS0564" + - "CS0567" + - "CS0590" + - "CS0660" + - "CS0661" + - "CS0715" + - "CS1037" + - "CS1553" + - "CS8930" + - "CS8931" + - "CS9023" + - "CS9024" + - "CS9025" + - "CS9308" + - "CS9310" + - "CS9311" + - "CS9312" + - "CS9313" +ms.date: 10/15/2025 +ai-usage: ai-assisted +--- +# Errors and warnings for overloaded, or user-defined operator declarations + +There are several errors related to declaring overloaded operators. Overloaded operators are also referred to as user-defined operators + + +- [**CS0056**](#inconsistent-accessibility): *Inconsistent accessibility: return type 'type' is less accessible than operator 'operator'* +- [**CS0057**](#inconsistent-accessibility): *Inconsistent accessibility: parameter type 'type' is less accessible than operator 'operator'* +- [**CS0215**](#boolean-and-short-circuit-operators): *The return type of operator True or False must be bool* +- [**CS0216**](#boolean-and-short-circuit-operators): *The operator 'operator' requires a matching operator 'missing_operator' to also be defined* +- [**CS0217**](#boolean-and-short-circuit-operators): *In order to be applicable as a short circuit operator a user-defined logical operator ('operator') must have the same return type as the type of its 2 parameters.* +- [**CS0218**](#boolean-and-short-circuit-operators): *The type ('type') must contain declarations of operator true and operator false* +- [**CS0448**](#operator-signature-requirements): *The return type for `++` or `--` operator must be the containing type or derived from the containing type* +- [**CS0552**](#user-defined-conversion-restrictions): *'conversion routine' : user defined conversion to/from interface* +- [**CS0553**](#user-defined-conversion-restrictions): *'conversion routine' : user defined conversion to/from base class* +- [**CS0554**](#user-defined-conversion-restrictions): *'conversion routine' : user defined conversion to/from derived class* +- [**CS0555**](#user-defined-conversion-restrictions): *User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type* +- [**CS0556**](#user-defined-conversion-restrictions): *User-defined conversion must convert to or from the enclosing type* +- [**CS0557**](#user-defined-conversion-restrictions): *Duplicate user-defined conversion in type* +- [**CS0558**](#operator-declaration-requirements): *User-defined operator must be declared static and public* +- [**CS0559**](#operator-signature-requirements): *The parameter type for `++` or `--` operator must be the containing type* +- [**CS0562**](#operator-signature-requirements): *The parameter of a unary operator must be the containing type* +- [**CS0563**](#operator-signature-requirements): *One of the parameters of a binary operator must be the containing type* +- [**CS0564**](#operator-signature-requirements): *The first operand of an overloaded shift operator must have the same type as the containing type, and the type of the second operand must be int* +- [**CS0567**](#operator-signature-requirements): *Interfaces cannot contain operators* +- [**CS0590**](#operator-signature-requirements): *User-defined operators cannot return void* +- [**CS0660**](#equality-operators): *Type defines `operator ==` or `operator !=` but does not override `Object.Equals(object o)`* +- [**CS0661**](#equality-operators): *Type defines `operator ==` or `operator !=` but does not override `Object.GetHashCode()`* +- [**CS0715**](#operator-declaration-requirements): *Static classes cannot contain user-defined operators* +- [**CS1037**](#operator-declaration-requirements): *Overloadable operator expected* +- [**CS1553**](#operator-declaration-requirements): *Declaration is not valid; use 'modifier operator \ (...' instead* +- [**CS8930**](#operator-declaration-requirements): *Explicit implementation of a user-defined operator must be static.* +- [**CS8931**](#operator-declaration-requirements): *Explicit implementation must be declared public to implement interface member in type.* +- [**CS9023**](#checked-operators): *Operator cannot be made checked.* +- [**CS9024**](#checked-operators): *Operator cannot be made unchecked.* +- [**CS9025**](#checked-operators): *Operator requires a matching non-checked version to also be declared.* +- [**CS9308**](#operator-declaration-requirements): *User-defined operator must be declared public.* +- [**CS9310**](#operator-signature-requirements): *The return type for this operator must be void.* +- [**CS9311**](#interface-and-inheritance-requirements): *Type does not implement interface member. The type cannot implement member because one of them is not an operator.* +- [**CS9312**](#interface-and-inheritance-requirements): *Type cannot override inherited member because one of them is not an operator.* +- [**CS9313**](#interface-and-inheritance-requirements): *Overloaded compound assignment operator takes one parameter.* + +The following sections provide examples of common issues and how to fix them. + +## Operator signature requirements + +- **CS0448**: *The return type for `++` or `--` operator must be the containing type or derived from the containing type.* +- **CS0559**: *The parameter type for `++` or `--` operator must be the containing type.* +- **CS0562**: *The parameter of a unary operator must be the containing type.* +- **CS0563**: *One of the parameters of a binary operator must be the containing type.* +- **CS0564**: *The first operand of an overloaded shift operator must have the same type as the containing type, and the type of the second operand must be int.* +- **CS0567**: *Interfaces can't contain operators.* +- **CS0590**: *User-defined operators can't return void.* +- **CS9310**: *The return type for this operator must be void.* + +These errors occur when operator declarations don't follow the required signature rules. Each operator type has specific requirements for parameter types and return types. + +> [!IMPORTANT] +> The signature requirements for static binary operators and the corresponding instance compound assignment operators are different. Make sure the signature matches the declaration you want. + +For more information, see [Operator overloading](../operators/operator-overloading.md). The following example demonstrates these errors: + +```csharp +class C1 +{ + public static int operator ++(C1 c) => 0; // CS0448 + public static C1 operator --(C1 c) => null; // OK +} +public class C2 +{ + public static implicit operator int(C2 x) => 0; + public static implicit operator C2(int x) => new C2(); + public static int operator ++(int aa) => 0; // CS0559 +} +public class C3 +{ + public static implicit operator int(C3 x) => 0; + public static implicit operator C3(int x) => null; + public static C3 operator +(int aa) => 0; // CS0562 +} +public class C4 +{ + public static implicit operator int(C4 x) => 0; + public static implicit operator C4(int x) => null; + public static int operator +(int aa, int bb) => 0; // CS0563 +} +class C5 +{ + // To correct, change second operand to int, like so: + // public static int operator << (C c1, int c2) + public static int operator <<(C5 c1, C5 c2) => 0; // CS0564 +} +interface IA +{ + int operator +(int aa, int bb); // CS0567 +} +public class C6 +{ + public static void operator +(C6 A1, C6 A2) { } // CS0590 +} +``` + +To fix these errors, ensure your operator declarations follow the signature requirements for the specific operator type you're overloading. + +## Operator declaration requirements + +- **CS0558**: *User-defined operator must be declared static and public.* +- **CS0715**: *Static classes can't contain user-defined operators.* +- **CS1037**: *Overloadable operator expected.* +- **CS1553**: *Declaration isn't valid; use 'modifier operator \ (...' instead.* +- **CS8930**: *Explicit implementation of a user-defined operator must be static.* +- **CS8931**: *Explicit implementation must be declared public to implement interface member in type.* +- **CS9308**: *User-defined operator must be declared public.* + +These errors occur when operator declarations don't use the required modifiers or syntax. Most user-defined operators must be both `static` and `public`, and conversion operators require specific syntax. For more information, see [Operator overloading](../operators/operator-overloading.md) and [User-defined conversion operators](../operators/user-defined-conversion-operators.md). The following code demonstrates these errors: + +```csharp +public class C +{ + static implicit operator int(C aa) => 0; // CS0558, add public +} +public static class C1 +{ + public static int operator +(C1 c) => 0; // CS0715 +} +class C2 +{ + public static int implicit operator (C2 f) => 6; // CS1553 +} +``` + +To fix these errors, ensure your operator declarations include the required `static` and `public` modifiers, follow the correct syntax for conversion operators, and don't declare operators in static classes. + +## Inconsistent accessibility + +- **CS0056**: *Inconsistent accessibility: return type 'type' is less accessible than operator 'operator'.* +- **CS0057**: *Inconsistent accessibility: parameter type 'type' is less accessible than operator 'operator'.* + +These errors occur when you declare a public operator with return types or parameter types that have more restrictive accessibility than the operator itself. All public constructs must use publicly accessible types for their parameters and return values. For more information, see [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md). + +The following code snippets demonstrate these errors: + +```csharp +class C { } + +public class C2 +{ + public static implicit operator C(C2 a) => new C(); // CS0056 +} + +public class C3 +{ + public static implicit operator C3(C c) => new C3(); // CS0057 +} +``` + +To fix these errors, make sure all types used in public operator declarations are also publicly accessible. + +## User-defined conversion restrictions + +- **CS0552**: *User-defined conversion to/from interface.* +- **CS0553**: *User-defined conversion to/from base class.* +- **CS0554**: *User-defined conversion to/from derived class.* +- **CS0555**: *User-defined operator can't take an object of the enclosing type and convert to an object of the enclosing type.* +- **CS0556**: *User-defined conversion must convert to or from the enclosing type.* +- **CS0557**: *Duplicate user-defined conversion in type.* + +These errors occur when you attempt to create invalid user-defined conversion operators. Conversion operators have specific restrictions about which types they can convert between. For more information, see [User-defined conversion operators](../operators/user-defined-conversion-operators.md). The following code demonstrates the preceding errors: + +```csharp +public interface I +{ +} +public class C +{ + public static implicit operator I(C aa) => default;// CS0552 +} + +public class B +{ +} +public class D : B +{ + public static implicit operator B(D aa) => new B();// CS0553 +} + +public class B2 +{ + // delete the conversion routine to resolve CS0554 + public static implicit operator B2(D2 d) => new B2();// CS0554 +} +public class D2 : B2 { } + +public class C2 +{ + public static implicit operator C2(C2 aa) => new C2(); // CS0555 +} + +public class C3 +{ + public static implicit operator int(byte aa) => 0; // CS0556 +} + +public class C4 +{ + public static implicit operator int(C4 aa) => 0; + + // CS0557, delete duplicate + public static explicit operator int(C4 aa) => 0; +} +``` + +To fix these errors, remove invalid conversion operators or restructure your type hierarchy to avoid the restricted conversion patterns. + +## Boolean and short-circuit operators + +- **CS0215**: *The return type of operator true or false must be bool.* +- **CS0216**: *The operator requires a matching operator to also be defined.* +- **CS0217**: *In order to be applicable as a short-circuit operator, a user-defined logical operator must have the same return type as the type of its 2 parameters.* +- **CS0218**: *The type must contain declarations of operator true and operator false.* + +These errors occur when you define logical operators incorrectly. Certain operators must be defined in pairs, and short-circuit operators have specific signature requirements. For more information, see [true and false operators](../operators/true-false-operators.md), [Boolean logical operators](../operators/boolean-logical-operators.md), and [User-defined conditional logical operators](~/_csharpstandard/standard/expressions.md#12153-user-defined-conditional-logical-operators). The following code demonstrates these errors: + +```csharp +class C +{ + public static int operator true(C c) => 0; // CS0215 + public static int operator false(C c) => 0; // CS0215 +} + +class C2 +{ + public static bool operator ==(C2 left, C2 right) => left.Equals(right); // CS0216 + + public override bool Equals(object? o) => base.Equals(o); + public override int GetHashCode() => base.GetHashCode(); +} + +public class C3 +{ + public static bool operator true(C3 f) => false; + public static bool operator false(C3 f) => true; + public static implicit operator int(C3 x) => 0; + public static int operator &(C3 f1, C3 f2) => new C3(); // CS0217 +} + +public class C4 +{ + public static implicit operator int(C4 x) => 0; + public static C4 operator &(C4 f1, C4 f2) => new C4(); + + public static void Main() + { + C4 f = new C4(); + int i = f && f; // CS0218, requires operators true and false + } +} +``` + +To fix these errors, ensure you define required paired operators and follow the correct signature patterns for logical operators. + +## Checked operators + +- **CS9023**: *Operator can't be made checked* +- **CS9024**: *Operator can't be made unchecked* +- **CS9025**: *Checked operator requires a matching non-checked version to also be declared* + +These errors occur when you incorrectly use the `checked` or `unchecked` keywords with operator declarations. Not all operators support checked/unchecked variants, and when they do, certain requirements must be met. For more information, see [Arithmetic operators](../operators/arithmetic-operators.md#user-defined-checked-operators) and [User-defined checked operators](~/_csharplang/proposals/csharp-11.0/checked-user-defined-operators.md). + +To fix these errors, either remove the `checked` or `unchecked` keyword from operators that don't support it, or ensure you provide both checked and non-checked versions when required. + +## Interface and inheritance requirements + +- **CS9311**: *Type doesn't implement interface member. The type can't implement member because one of them isn't an operator* +- **CS9312**: *Type can't override inherited member because one of them isn't an operator* +- **CS9313**: *Overloaded compound assignment operator takes one parameter* + +These errors occur when there are mismatches between operator declarations and interface implementations or inheritance relationships. Operators have specific rules for interface implementation and overriding. For more information, see [Operator overloading](../operators/operator-overloading.md) and [Interfaces](../../fundamentals/types/interfaces.md). + +To fix these errors, ensure that operator declarations correctly match interface requirements and follow the rules for operator overriding and compound assignment operators. + +## Equality operators + +- **CS0660**: *Type defines operator == or operator != but doesn't override Object.Equals(object o)* +- **CS0661**: *Type defines operator == or operator != but doesn't override Object.GetHashCode()* + +These warnings occur when you define equality or inequality operators without also overriding the corresponding methods from . When you define custom equality comparison, you should also override and to ensure consistent behavior. For more information, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) and [Equality operators](../operators/equality-operators.md). + +To fix these warnings, override both `Equals` and `GetHashCode` when you define custom equality operators. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 7e3f7d8b51acd..92f54bc76877b 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -484,6 +484,13 @@ items: CS0514, CS0515, CS0516, CS0517, CS0522, CS0526, CS0568, CS0710, CS0768, CS0824, CS8054, CS8091, CS8358, CS8862, CS8867, CS8868, CS8878, CS8910, CS8958, CS8982, CS8983, CS9105, CS9106, CS9107, CS9108, CS9109, CS9110, CS9111, CS9112, CS9113, CS9114, CS9115, CS9116, CS9117, CS9118, CS9119, CS9120, CS9121, CS9122, CS9124, CS9136, CS9179 + - name: Operator declarations + href: ./compiler-messages/overloaded-operator-errors.md + displayName: > + operator, overloaded operator, user defined operator, + CS0056, CS0057, CS0215, CS0216, CS0217, CS0218, CS0448, CS0552, CS0553, CS0554, CS0555, CS0556, CS0557, CS0558, CS0559, + CS0562, CS0563, CS0564, CS0567, CS0590, CS0660, CS0661, CS0715, CS1037, CS1553, CS1554, CS8930, CS8931, CS9023, CS9024, + CS9025, CS9308, CS9310, CS9311, CS9312, CS9313 - name: Parameter / argument mismatch href: ./compiler-messages/parameter-argument-mismatch.md displayName: > @@ -669,10 +676,6 @@ items: href: ../misc/cs0054.md - name: CS0055 href: ../misc/cs0055.md - - name: CS0056 - href: ../misc/cs0056.md - - name: CS0057 - href: ../misc/cs0057.md - name: CS0058 href: ../misc/cs0058.md - name: CS0059 @@ -867,14 +870,6 @@ items: href: ../misc/cs0213.md - name: CS0214 href: ../misc/cs0214.md - - name: CS0215 - href: ../misc/cs0215.md - - name: CS0216 - href: ../misc/cs0216.md - - name: CS0217 - href: ../misc/cs0217.md - - name: CS0218 - href: ../misc/cs0218.md - name: CS0220 href: ../misc/cs0220.md - name: CS0221 @@ -1027,8 +1022,6 @@ items: href: ./compiler-messages/cs0446.md - name: CS0447 href: ../misc/cs0447.md - - name: CS0448 - href: ../misc/cs0448.md - name: CS0449 href: ../misc/cs0449.md - name: CS0450 @@ -1135,30 +1128,6 @@ items: href: ../misc/cs0550.md - name: CS0551 href: ../misc/cs0551.md - - name: CS0552 - href: ./compiler-messages/cs0552.md - - name: CS0553 - href: ../misc/cs0553.md - - name: CS0554 - href: ../misc/cs0554.md - - name: CS0555 - href: ../misc/cs0555.md - - name: CS0556 - href: ../misc/cs0556.md - - name: CS0557 - href: ../misc/cs0557.md - - name: CS0558 - href: ../misc/cs0558.md - - name: CS0559 - href: ../misc/cs0559.md - - name: CS0562 - href: ../misc/cs0562.md - - name: CS0563 - href: ./compiler-messages/cs0563.md - - name: CS0564 - href: ../misc/cs0564.md - - name: CS0567 - href: ../misc/cs0567.md - name: CS0569 href: ../misc/cs0569.md - name: CS0570 @@ -1195,8 +1164,6 @@ items: href: ../misc/cs0588.md - name: CS0589 href: ../misc/cs0589.md - - name: CS0590 - href: ../misc/cs0590.md - name: CS0592 href: ./compiler-messages/cs0592.md - name: CS0594 @@ -1311,8 +1278,6 @@ items: href: ../misc/cs0713.md - name: CS0714 href: ../misc/cs0714.md - - name: CS0715 - href: ../misc/cs0715.md - name: CS0716 href: ../misc/cs0716.md - name: CS0717 @@ -1451,8 +1416,6 @@ items: href: ../misc/cs1035.md - name: CS1036 href: ../misc/cs1036.md - - name: CS1037 - href: ../misc/cs1037.md - name: CS1038 href: ../misc/cs1038.md - name: CS1040 @@ -1541,10 +1504,6 @@ items: href: ./compiler-messages/cs1548.md - name: CS1551 href: ../misc/cs1551.md - - name: CS1553 - href: ../misc/cs1553.md - - name: CS1554 - href: ../misc/cs1554.md - name: CS1555 href: ../misc/cs1555.md - name: CS1556 @@ -2235,10 +2194,6 @@ items: href: ../misc/cs0642.md - name: CS0659 href: ../misc/cs0659.md - - name: CS0660 - href: ../misc/cs0660.md - - name: CS0661 - href: ../misc/cs0661.md - name: CS0665 href: ../misc/cs0665.md - name: CS0675 diff --git a/docs/csharp/misc/cs0056.md b/docs/csharp/misc/cs0056.md deleted file mode 100644 index db4f9a1055111..0000000000000 --- a/docs/csharp/misc/cs0056.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -description: "Compiler Error CS0056" -title: "Compiler Error CS0056" -ms.date: 07/20/2015 -f1_keywords: - - "CS0056" -helpviewer_keywords: - - "CS0056" -ms.assetid: 8878b09c-5b7b-40e0-be0d-61ef5b36c151 ---- -# Compiler Error CS0056 - -Inconsistent accessibility: return type 'type' is less accessible than operator 'operator' - - A public construct must return a publicly accessible object. For more information, see [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md). - - The following sample generates CS0056: - -```csharp -// CS0056.cs -class MyClass -// try the following line instead -// public class MyClass -{ -} - -public class A -{ - public static implicit operator MyClass(A a) // CS0056 - { - return new MyClass(); - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0057.md b/docs/csharp/misc/cs0057.md deleted file mode 100644 index c2d3e560fc210..0000000000000 --- a/docs/csharp/misc/cs0057.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -description: "Compiler Error CS0057" -title: "Compiler Error CS0057" -ms.date: 07/20/2015 -f1_keywords: - - "CS0057" -helpviewer_keywords: - - "CS0057" -ms.assetid: 0bdd628f-7a1f-4209-bb28-c4a66eb3bf1d ---- -# Compiler Error CS0057 - -Inconsistent accessibility: parameter type 'type' is less accessible than operator 'operator' - - A public construct must return a publicly accessible object. For more information, see [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md). - - The following sample generates CS0057: - -```csharp -// CS0057.cs -class MyClass //defaults to private accessibility -// try the following line instead -// public class MyClass -{ -} - -public class MyClass2 -{ - public static implicit operator MyClass2(MyClass iii) // CS0057 - { - return new MyClass2(); - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0215.md b/docs/csharp/misc/cs0215.md deleted file mode 100644 index d34e09ff46409..0000000000000 --- a/docs/csharp/misc/cs0215.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -description: "Compiler Error CS0215" -title: "Compiler Error CS0215" -ms.date: 07/20/2015 -f1_keywords: - - "CS0215" -helpviewer_keywords: - - "CS0215" -ms.assetid: 2060440d-be22-4c10-8b26-43b08b615447 ---- -# Compiler Error CS0215 - -The return type of operator True or False must be bool - -User-defined [true and false](../language-reference/operators/true-false-operators.md) operators must have a return type of [bool](../language-reference/builtin-types/bool.md). - -The following sample generates CS0215: - -```csharp -// CS0215.cs -class MyClass -{ - public static int operator true (MyClass MyInt) // CS0215 - // try the following line instead - // public static bool operator true (MyClass MyInt) - { - return true; - } - - public static int operator false (MyClass MyInt) // CS0215 - // try the following line instead - // public static bool operator false (MyClass MyInt) - { - return true; - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0216.md b/docs/csharp/misc/cs0216.md deleted file mode 100644 index d695d4e170acb..0000000000000 --- a/docs/csharp/misc/cs0216.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -description: "Compiler Error CS0216" -title: "Compiler Error CS0216" -ms.date: 07/20/2015 -f1_keywords: - - "CS0216" -helpviewer_keywords: - - "CS0216" -ms.assetid: afb3dd29-3eff-4b62-8267-eb726c2bcee4 ---- -# Compiler Error CS0216 - -The operator 'operator' requires a matching operator 'missing_operator' to also be defined - - A user-defined [==](../language-reference/operators/equality-operators.md#equality-operator-) operator requires a user-defined [!=](../language-reference/operators/equality-operators.md#inequality-operator-) operator, and vice versa. - The same applies also to a user-defined [true](../language-reference/operators/true-false-operators.md) operator and a user-defined [false](../language-reference/operators/true-false-operators.md) operator. - - The following sample generates CS0216: - -```csharp -// CS0216.cs -class MyClass -{ - public static bool operator == (MyClass MyIntLeft, MyClass MyIntRight) // CS0216 - { - return MyIntLeft == MyIntRight; - } - - // to resolve, uncomment the following operator definition - /* - public static bool operator != (MyClass MyIntLeft, MyClass MyIntRight) - { - return MyIntLeft != MyIntRight; - } - */ - - public override bool Equals (object obj) - { - return base.Equals (obj); - } - - public override int GetHashCode() - { - return base.GetHashCode(); - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0217.md b/docs/csharp/misc/cs0217.md deleted file mode 100644 index e0bb38ad888cb..0000000000000 --- a/docs/csharp/misc/cs0217.md +++ /dev/null @@ -1,58 +0,0 @@ ---- -description: "Compiler Error CS0217" -title: "Compiler Error CS0217" -ms.date: 07/20/2015 -f1_keywords: - - "CS0217" -helpviewer_keywords: - - "CS0217" -ms.assetid: ede61095-6e11-4f4a-8e7d-85e7a3f4fc3d ---- -# Compiler Error CS0217 - -In order to be applicable as a short circuit operator a user-defined logical operator ('operator') must have the same return type as the type of its 2 parameters. - - If you define an operator for a user-defined type, and then try to use the operator as a short-circuit operator, the user-defined operator must have parameters and return values of the same type. For more information about short-circuit operators, see [`&&` operator](../language-reference/operators/boolean-logical-operators.md#conditional-logical-and-operator-) and [`||` operator](../language-reference/operators/boolean-logical-operators.md#conditional-logical-or-operator-). For more information about user-defined short-circuit, or conditional, operators, see the [User-defined conditional logical operators](~/_csharpstandard/standard/expressions.md#12153-user-defined-conditional-logical-operators ) section of the [C# language specification](~/_csharpstandard/standard/README.md). - - The following sample generates CS0217: - -```csharp -// CS0217.cs -using System; - -public class MyClass -{ - public static bool operator true (MyClass f) - { - return false; - } - - public static bool operator false (MyClass f) - { - return false; - } - - public static implicit operator int(MyClass x) - { - return 0; - } - - public static int operator & (MyClass f1, MyClass f2) // CS0217 - // try the following line instead - // public static MyClass operator & (MyClass f1, MyClass f2) - { - return new MyClass(); - } - - public static void Main() - { - MyClass f = new MyClass(); - int i = f && f; - } -} -``` - -## See also - -- [Operator overloading](../language-reference/operators/operator-overloading.md) -- [true and false operators](../language-reference/operators/true-false-operators.md) diff --git a/docs/csharp/misc/cs0218.md b/docs/csharp/misc/cs0218.md deleted file mode 100644 index 072d5a070e252..0000000000000 --- a/docs/csharp/misc/cs0218.md +++ /dev/null @@ -1,57 +0,0 @@ ---- -description: "Compiler Error CS0218" -title: "Compiler Error CS0218" -ms.date: 07/20/2015 -f1_keywords: - - "CS0218" -helpviewer_keywords: - - "CS0218" -ms.assetid: f675e06a-c55c-44a1-b5db-0df178fd8f79 ---- -# Compiler Error CS0218 - -The type ('type') must contain declarations of operator true and operator false - -If a user-defined type overloads the [& operator](../language-reference/operators/boolean-logical-operators.md#logical-and-operator-) or [| operator](../language-reference/operators/boolean-logical-operators.md#logical-or-operator-), it must also define [true and false](../language-reference/operators/true-false-operators.md) operators, in order to make short-circuiting [&& operator](../language-reference/operators/boolean-logical-operators.md#conditional-logical-and-operator-) or [|| operator](../language-reference/operators/boolean-logical-operators.md#conditional-logical-or-operator-) defined. - - The following sample generates CS0218: - -```csharp -// CS0218.cs -using System; -public class MyClass -{ - // uncomment these operator declarations to resolve this CS0218 - /* - public static bool operator true (MyClass f) - { - return false; - } - - public static bool operator false (MyClass f) - { - return false; - } - */ - - public static implicit operator int(MyClass x) - { - return 0; - } - - public static MyClass operator & (MyClass f1, MyClass f2) - { - return new MyClass(); - } - - public static void Main() - { - MyClass f = new MyClass(); - int i = f && f; // CS0218, requires operators true and false - } -} -``` - -## See also - -- [Operator overloading](../language-reference/operators/operator-overloading.md) diff --git a/docs/csharp/misc/cs0448.md b/docs/csharp/misc/cs0448.md deleted file mode 100644 index cae42f3dae3c8..0000000000000 --- a/docs/csharp/misc/cs0448.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -description: "Compiler Error CS0448" -title: "Compiler Error CS0448" -ms.date: 07/20/2015 -f1_keywords: - - "CS0448" -helpviewer_keywords: - - "CS0448" -ms.assetid: f577ab4c-1c8c-4a10-80a8-9ba9f66c3d25 ---- -# Compiler Error CS0448 - -The return type for ++ or -- operator must be the containing type or derived from the containing type - - When you override the `++` or `--` operators, they must return the same type as the containing type, or return a type that is derived from the containing type. - -## Example 1 - - The following sample generates CS0448. - -```csharp -// CS0448.cs -class C5 -{ - public static int operator ++(C5 c) { return null; } // CS0448 - public static C5 operator --(C5 c) { return null; } // OK - public static void Main() {} -} -``` - -## Example 2 - - The following sample generates CS0448. - -```csharp -// CS0448_b.cs -public struct S -{ - public static S? operator ++(S s) { return new S(); } // CS0448 - public static S? operator --(S s) { return new S(); } // CS0448 -} - -public struct T -{ -// OK - public static T operator --(T t) { return new T(); } - public static T operator ++(T t) { return new T(); } - - public static T? operator --(T? t) { return new T(); } - public static T? operator ++(T? t) { return new T(); } - - public static void Main() {} -} -``` diff --git a/docs/csharp/misc/cs0553.md b/docs/csharp/misc/cs0553.md deleted file mode 100644 index ccdbb0a1c50e9..0000000000000 --- a/docs/csharp/misc/cs0553.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0553" -title: "Compiler Error CS0553" -ms.date: 07/20/2015 -f1_keywords: - - "CS0553" -helpviewer_keywords: - - "CS0553" -ms.assetid: d2d6ddb1-9294-4e85-83d8-c35bd7a70f5b ---- -# Compiler Error CS0553 - -'conversion routine' : user defined conversion to/from base class - - User-defined conversions to values of a base class are not allowed; you do not need such an operator. - - The following sample generates CS0553: - -```csharp -// CS0553.cs -namespace x -{ - public class ii - { - } - - public class a : ii - { - // delete the conversion routine to resolve CS0553 - public static implicit operator ii(a aa) // CS0553 - { - return new ii(); - } - - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0554.md b/docs/csharp/misc/cs0554.md deleted file mode 100644 index 3c93d8e1f822f..0000000000000 --- a/docs/csharp/misc/cs0554.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -description: "Compiler Error CS0554" -title: "Compiler Error CS0554" -ms.date: 07/20/2015 -f1_keywords: - - "CS0554" -helpviewer_keywords: - - "CS0554" -ms.assetid: 884db4b2-3a69-4434-9a25-526f596e03c8 ---- -# Compiler Error CS0554 - -'conversion routine' : user defined conversion to/from derived class - - User-defined conversions to values of a derived class are not allowed; you do not need such an operator. - - See chapter 6 in the C# language specification for more information on user-defined conversions. - - The following sample generates CS0554: - -```csharp -// CS0554.cs -namespace x -{ - public class ii - { - // delete the conversion routine to resolve CS0554 - public static implicit operator ii(a aa) // CS0554 - { - return new ii(); - } - } - - public class a : ii - { - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0555.md b/docs/csharp/misc/cs0555.md deleted file mode 100644 index 09d40004014b6..0000000000000 --- a/docs/csharp/misc/cs0555.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS0555" -title: "Compiler Error CS0555" -ms.date: 07/20/2015 -f1_keywords: - - "CS0555" -helpviewer_keywords: - - "CS0555" -ms.assetid: e4b2f890-98b4-4578-b1de-ebaafc8b3da2 ---- - -# Compiler Error CS0555 - -User-defined operator cannot take an object of the enclosing type and convert to an object of the enclosing type - -User-defined conversions to values of the enclosing class are not allowed; you do not need such an operator. - -The following sample generates CS0555: - -```csharp -// CS0555.cs -public class MyClass -{ - // delete the following operator to resolve this CS0555 - public static implicit operator MyClass(MyClass aa) // CS0555 - { - return new MyClass(); - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0556.md b/docs/csharp/misc/cs0556.md deleted file mode 100644 index efb706426cb60..0000000000000 --- a/docs/csharp/misc/cs0556.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS0556" -title: "Compiler Error CS0556" -ms.date: 07/20/2015 -f1_keywords: - - "CS0556" -helpviewer_keywords: - - "CS0556" -ms.assetid: e2430c6e-784f-4ab2-88b9-f660d956e9e8 ---- -# Compiler Error CS0556 - -User-defined conversion must convert to or from the enclosing type - - A user-defined conversion routine must convert to or from the class that contains the routine. - - The following sample generates CS0556: - -```csharp -// CS0556.cs -namespace x -{ - public class ii - { - public class iii - { - public static implicit operator int(byte aa) // CS0556 - // try the following line instead - // public static implicit operator int(iii aa) - { - return 0; - } - } - - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0557.md b/docs/csharp/misc/cs0557.md deleted file mode 100644 index dc37957c13f79..0000000000000 --- a/docs/csharp/misc/cs0557.md +++ /dev/null @@ -1,44 +0,0 @@ ---- -description: "Compiler Error CS0557" -title: "Compiler Error CS0557" -ms.date: 07/20/2015 -f1_keywords: - - "CS0557" -helpviewer_keywords: - - "CS0557" -ms.assetid: beca353e-4fea-4e4f-a48a-eddeebb153bb ---- -# Compiler Error CS0557 - -Duplicate user-defined conversion in type 'class' - - Duplicate conversion routines are not allowed in a class. - - The following example generates CS0557: - -```csharp -// CS0557.cs -namespace x -{ - public class ii - { - public class iii - { - public static implicit operator int(iii aa) - { - return 0; - } - - // CS0557, delete duplicate - public static explicit operator int(iii aa) - { - return 0; - } - } - - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0558.md b/docs/csharp/misc/cs0558.md deleted file mode 100644 index e19c5587adfcd..0000000000000 --- a/docs/csharp/misc/cs0558.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -description: "Compiler Error CS0558" -title: "Compiler Error CS0558" -ms.date: 07/20/2015 -f1_keywords: - - "CS0558" -helpviewer_keywords: - - "CS0558" -ms.assetid: af63b9ba-2790-4362-a49d-b69a5292a555 ---- -# Compiler Error CS0558 - -User-defined operator 'operator' must be declared static and public - - Both the **static** and **public** access [modifiers](../language-reference/keywords/index.md) must be specified on user-defined operators. - - The following sample generates CS0558: - -```csharp -// CS0558.cs -namespace x -{ - public class ii - { - public class iii - { - static implicit operator int(iii aa) // CS0558, add public - { - return 0; - } - } - - public static void Main() - { - } - } -} -``` diff --git a/docs/csharp/misc/cs0559.md b/docs/csharp/misc/cs0559.md deleted file mode 100644 index 47f6e8b0dc023..0000000000000 --- a/docs/csharp/misc/cs0559.md +++ /dev/null @@ -1,67 +0,0 @@ ---- -description: "Compiler Error CS0559" -title: "Compiler Error CS0559" -ms.date: 07/20/2015 -f1_keywords: - - "CS0559" -helpviewer_keywords: - - "CS0559" -ms.assetid: 37122001-8a55-4cf5-873b-68997e196893 ---- -# Compiler Error CS0559 - -The parameter type for ++ or -- operator must be the containing type - - The method declaration for an operator overload must follow certain guidelines. For the ++ and -- operators, it is required that the parameter be of the same type as the type in which the operator is being overloaded. - -## Example 1 - - The following sample generates CS0559: - -```csharp -// CS0559.cs -// compile with: /target:library -public class iii -{ - public static implicit operator int(iii x) - { - return 0; - } - - public static implicit operator iii(int x) - { - return null; - } - - public static int operator ++(int aa) // CS0559 - // try the following line instead - // public static iii operator ++(iii aa) - { - return (iii)0; - } -} -``` - -## Example 2 - - The following sample generates CS0559. - -```csharp -// CS0559_b.cs -// compile with: /target:library -public struct S -{ - public static S operator ++(S? s) { return new S(); } // CS0559 - public static S operator --(S? s) { return new S(); } // CS0559 -} - -public struct T -{ -// OK - public static T operator --(T t) { return new T(); } - public static T operator ++(T t) { return new T(); } - - public static T? operator --(T? t) { return new T(); } - public static T? operator ++(T? t) { return new T(); } -} -``` diff --git a/docs/csharp/misc/cs0562.md b/docs/csharp/misc/cs0562.md deleted file mode 100644 index 377f37487725a..0000000000000 --- a/docs/csharp/misc/cs0562.md +++ /dev/null @@ -1,44 +0,0 @@ ---- -description: "Compiler Error CS0562" -title: "Compiler Error CS0562" -ms.date: 07/20/2015 -f1_keywords: - - "CS0562" -helpviewer_keywords: - - "CS0562" -ms.assetid: dceecce5-90ce-4554-820c-f4c06b2b8258 ---- -# Compiler Error CS0562 - -The parameter of a unary operator must be the containing type - -The method declaration for an operator overload must follow certain guidelines. For more information, see [Operator overloading](../language-reference/operators/operator-overloading.md). - -The following sample generates CS0562: - -```csharp -// CS0562.cs -public class iii -{ - public static implicit operator int(iii x) - { - return 0; - } - - public static implicit operator iii(int x) - { - return null; - } - - public static iii operator +(int aa) // CS0562 - // try the following line instead - // public static iii operator +(iii aa) - { - return (iii)0; - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0564.md b/docs/csharp/misc/cs0564.md deleted file mode 100644 index 37b1f083fa291..0000000000000 --- a/docs/csharp/misc/cs0564.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS0564" -title: "Compiler Error CS0564" -ms.date: 07/20/2015 -f1_keywords: - - "CS0564" -helpviewer_keywords: - - "CS0564" -ms.assetid: 4c152e10-eb22-4437-a85f-1599c76470e0 ---- -# Compiler Error CS0564 - -The first operand of an overloaded shift operator must have the same type as the containing type, and the type of the second operand must be int - - You attempted to overload a shift operator (<\< or >>) with incorrectly typed operands. The first operand must be the type and the second operand must be of the type `int`. - - The following sample generates CS0564: - -```csharp -// CS0564.cs -using System; -class C -{ - public static int operator << (C c1, C c2) // CS0564 -// To correct, change second operand to int, like so: -// public static int operator << (C c1, int c2) - { - return 0; - } - static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0567.md b/docs/csharp/misc/cs0567.md deleted file mode 100644 index af5ac3da40106..0000000000000 --- a/docs/csharp/misc/cs0567.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS0567" -title: "Compiler Error CS0567" -ms.date: 07/20/2015 -f1_keywords: - - "CS0567" -helpviewer_keywords: - - "CS0567" -ms.assetid: 90aefbf9-d216-4eb4-96d4-44926fa23b1e ---- -# Compiler Error CS0567 - -Interfaces cannot contain operators - - Operators are not permitted in [interface](../language-reference/keywords/interface.md) definitions. - - The following sample generates CS0567: - -```csharp -// CS0567.cs -interface IA -{ - int operator +(int aa, int bb); // CS0567 -} - -class Sample -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0590.md b/docs/csharp/misc/cs0590.md deleted file mode 100644 index 538270c39c124..0000000000000 --- a/docs/csharp/misc/cs0590.md +++ /dev/null @@ -1,43 +0,0 @@ ---- -description: "Compiler Error CS0590" -title: "Compiler Error CS0590" -ms.date: 07/20/2015 -f1_keywords: - - "CS0590" -helpviewer_keywords: - - "CS0590" -ms.assetid: 6df9461f-2de6-4032-b18f-96121db1e4af ---- -# Compiler Error CS0590 - -User-defined operators cannot return void - - The purpose of a user-defined operator is to return an object. - - The following sample generates CS0590: - -```csharp -// CS0590.cs -namespace x -{ - public class a - { - public static void operator+(a A1, a A2) // CS0590 - { - } - - // try the following user-defined operator - /* - public static a operator+(a A1, a A2) - { - return A2; - } - */ - - public static int Main() - { - return 1; - } - } -} -``` diff --git a/docs/csharp/misc/cs0660.md b/docs/csharp/misc/cs0660.md deleted file mode 100644 index a82634a5392cc..0000000000000 --- a/docs/csharp/misc/cs0660.md +++ /dev/null @@ -1,44 +0,0 @@ ---- -description: "Compiler Warning (level 3) CS0660" -title: "Compiler Warning (level 3) CS0660" -ms.date: 07/20/2015 -f1_keywords: - - "CS0660" -helpviewer_keywords: - - "CS0660" -ms.assetid: 2f77b45b-c5c6-46af-abe9-002e67887896 ---- -# Compiler Warning (level 3) CS0660 - -'class' defines operator == or operator != but does not override Object.Equals(object o) - -The compiler detected the user-defined equality or inequality operator, but no override for the method. A user-defined equality or inequality operator implies that you also want to override the method. For more information, see [How to define value equality for a type](../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md). - -The following sample generates CS0660: - -```csharp -// CS0660.cs -// compile with: /W:3 /warnaserror -class Test // CS0660 -{ - public static bool operator == (object o, Test t) - { - return true; - } - - // uncomment the Equals function to resolve - // public override bool Equals(object o) - // { - // return true; - // } - - public override int GetHashCode() - { - return 0; - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0661.md b/docs/csharp/misc/cs0661.md deleted file mode 100644 index 12ca927c95263..0000000000000 --- a/docs/csharp/misc/cs0661.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -description: "Compiler Warning (level 3) CS0661" -title: "Compiler Warning (level 3) CS0661" -ms.date: 07/20/2015 -f1_keywords: - - "CS0661" -helpviewer_keywords: - - "CS0661" -ms.assetid: c218665e-5947-40bb-b633-d268483e6522 ---- -# Compiler Warning (level 3) CS0661 - -'class' defines operator == or operator != but does not override Object.GetHashCode() - - The compiler detected the user-defined equality or inequality operator, but no override for the **GetHashCode** function. A user-defined equality or inequality operator implies that you also want to override the **GetHashCode** function. - - The following sample generates CS0661: - -```csharp -// CS0661.cs -// compile with: /W:3 -class Test // CS0661 -{ - public static bool operator == (object o, Test t) - { - return true; - } - - public static bool operator != (object o, Test t) - { - return true; - } - - public override bool Equals(object o) - { - return true; - } - - // uncomment the GetHashCode function to resolve - // public override int GetHashCode() - // { - // return 0; - // } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0715.md b/docs/csharp/misc/cs0715.md deleted file mode 100644 index fd403e34f1d9b..0000000000000 --- a/docs/csharp/misc/cs0715.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -description: "Compiler Error CS0715" -title: "Compiler Error CS0715" -ms.date: 07/20/2015 -f1_keywords: - - "CS0715" -helpviewer_keywords: - - "CS0715" -ms.assetid: e3cd1e46-ccfa-4678-a2ed-69245f3558ba ---- -# Compiler Error CS0715 - -'static class' : static classes cannot contain user defined operators - - User defined operators operate on instances of classes. Static classes cannot be instantiated, so it is not possible to create instances for operators to act upon. Hence, user defined operators are not allowed for static classes. - - The following sample generates CS0715: - -```csharp -// CS0715.cs -public static class C -{ - public static C operator+(C c) // CS0715 - { - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1037.md b/docs/csharp/misc/cs1037.md deleted file mode 100644 index 012459c724563..0000000000000 --- a/docs/csharp/misc/cs1037.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -description: "Compiler Error CS1037" -title: "Compiler Error CS1037" -ms.date: 07/20/2015 -f1_keywords: - - "CS1037" -helpviewer_keywords: - - "CS1037" -ms.assetid: 22e16a58-e77b-41cf-b4b5-b2603bb819c6 ---- -# Compiler Error CS1037 - -Overloadable operator expected - - When specifying a comment with [**DocumentationFile**](../language-reference/compiler-options/output.md#documentationfile), the compiler encountered an invalid link. diff --git a/docs/csharp/misc/cs1553.md b/docs/csharp/misc/cs1553.md deleted file mode 100644 index 4ebafaa7b329a..0000000000000 --- a/docs/csharp/misc/cs1553.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS1553" -title: "Compiler Error CS1553" -ms.date: 07/20/2015 -f1_keywords: - - "CS1553" -helpviewer_keywords: - - "CS1553" -ms.assetid: aec64251-b4ac-45c0-b143-7ebda138af6e ---- -# Compiler Error CS1553 - -Declaration is not valid; use 'modifier operator \ (...' instead - -The return type for a [conversion operator](../language-reference/operators/user-defined-conversion-operators.md) must immediately precede the parameter list, and *modifier* is either `implicit` or `explicit`. - -The following sample generates CS1553: - -```csharp -// CS1553.cs -class MyClass -{ - public static int implicit operator (MyClass f) // CS1553 - // try the following line instead - // public static implicit operator int (MyClass f) - { - return 6; - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1554.md b/docs/csharp/misc/cs1554.md deleted file mode 100644 index 352c2308f5c3f..0000000000000 --- a/docs/csharp/misc/cs1554.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -description: "Compiler Error CS1554" -title: "Compiler Error CS1554" -ms.date: 07/20/2015 -f1_keywords: - - "CS1554" -helpviewer_keywords: - - "CS1554" -ms.assetid: 81e8d4ac-cdbf-4b75-8932-0bc271a8405c ---- -# Compiler Error CS1554 - -Declaration is not valid; use '\ operator op (...' instead - -The return type of an [overloaded operator](../language-reference/operators/operator-overloading.md) must appear before the `operator` keyword. - -The following sample generates CS1554: - -```csharp -// CS1554.cs -class MyClass -{ - public static operator ++ MyClass (MyClass f) // CS1554 - // try the following line instead - // public static MyClass operator ++ (MyClass f) - { - return new MyClass (); - } - - 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 78ddbf37eee9d..c8f5d9aece3ea 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 @@ -513,9 +513,6 @@ f1_keywords: - "CS9020" - "CS9021" - "CS9022" - - "CS9023" - - "CS9024" - - "CS9025" - "CS9026" - "CS9027" - "CS9029" @@ -598,12 +595,6 @@ f1_keywords: # C# 14 errors begin here ## using `extension` as type name. (valid identifiers). - "CS9306" -## User defined operators - - "CS9308" - - "CS9310" - - "CS9311" - - "CS9312" - - "CS9313" ## More extension errors - "CS9316" - "CS9317" diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2224.md b/docs/fundamentals/code-analysis/quality-rules/ca2224.md index 9cd7d59ce412b..41510d644f12e 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca2224.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca2224.md @@ -29,7 +29,7 @@ A public type implements the equality operator but doesn't override method. If you implement the equality operator, its logic must be identical to that of . > [!NOTE] -> This rule only applies to Visual Basic code. The C# compiler generates a separate warning, [CS0660](../../../csharp/misc/cs0660.md). +> This rule only applies to Visual Basic code. The C# compiler generates a separate warning, [CS0660](../../../csharp/language-reference/compiler-messages/overloaded-operator-errors.md#equality-operators). ## How to fix violations @@ -78,4 +78,4 @@ The following example fixes the violation by overriding