From 7fa4ce0d478b2ccf8ab699e9b22e3c1327b3441f Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Sep 2024 15:01:48 -0400 Subject: [PATCH 1/9] Add new updates for Orpa Add the new compiler errors and warnings related to Overload Resolution Priority. --- .../compiler-messages/overload-resolution.md | 33 +++++++++++++++++++ docs/csharp/language-reference/toc.yml | 5 +++ 2 files changed, 38 insertions(+) create mode 100644 docs/csharp/language-reference/compiler-messages/overload-resolution.md diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md new file mode 100644 index 0000000000000..d59286f9bd26a --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -0,0 +1,33 @@ +--- +title: Fix errors that involve overload resolution +description: Compiler errors and warnings that indicate a problem in your code related to overload resolution. Learn causes and fixes for these errors. + - "CS9261" + - "CS9262" +helpviewer_keywords: + - "CS9261" + - "CS9262" +ms.date: 09/10/2024 +--- +# Resolve errors and warnings that impact overload resolution. + +This article covers the following compiler warnings: + +- [**CS9261**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* +- [**CS9262**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on this member.* + +## Overload resolution priority + +- **CS9261** - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* +- **CS9262** - *Cannot use '`OverloadResolutionPriorityAttribute`' on this member.* + +Your code violated the rules for using the to favor one overload instead of another. You can't apply the `OverloadResolutionPriorityAttribute` to the following method types: + +- Non-indexer properties +- Property, indexer, or event accessors +- Conversion operators +- Lambdas +- Local functions +- Finalizers +- Static constructors + +In addition, you can't apply the `OverloadResolutionPriorityAttribute` to an `override` of a `virtual` or `abstract` member. The compiler uses the value from the base type declaration. diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 09701d1614362..5562e3bfb7f9c 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -509,6 +509,11 @@ items: displayName: > CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098, CS9099, CS9100 + - name: Overload resolution + href: ./compiler-messages/overload-resolution-errors.md + displayName: > + 'overload resolution', + CS9261, CS9262 - name: Restrictions on expression trees href: ./compiler-messages/expression-tree-restrictions.md displayName: > From 6d0a55bc1bedaa15da702dcdd53561a1d814fba6 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Sep 2024 17:10:04 -0400 Subject: [PATCH 2/9] Move and consolidate content --- .openpublishing.redirection.csharp.json | 45 +++ .../array-declaration-errors.md | 17 + .../compiler-messages/cs1019.md | 65 --- .../compiler-messages/cs1063.md | 73 ---- .../compiler-messages/cs1501.md | 61 --- .../compiler-messages/overload-resolution.md | 369 ++++++++++++++++++ docs/csharp/language-reference/toc.yml | 32 +- docs/csharp/misc/cs0663.md | 35 -- docs/csharp/misc/cs1020.md | 32 -- docs/csharp/misc/cs1534.md | 35 -- docs/csharp/misc/cs1535.md | 36 -- docs/csharp/misc/cs1928.md | 47 --- docs/csharp/misc/cs1929.md | 102 ----- docs/csharp/misc/cs1950.md | 52 --- docs/csharp/misc/cs3006.md | 41 -- ...n-t-have-specifics-on-this-csharp-error.md | 2 - 16 files changed, 436 insertions(+), 608 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1019.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1063.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1501.md delete mode 100644 docs/csharp/misc/cs0663.md delete mode 100644 docs/csharp/misc/cs1020.md delete mode 100644 docs/csharp/misc/cs1534.md delete mode 100644 docs/csharp/misc/cs1535.md delete mode 100644 docs/csharp/misc/cs1928.md delete mode 100644 docs/csharp/misc/cs1929.md delete mode 100644 docs/csharp/misc/cs1950.md delete mode 100644 docs/csharp/misc/cs3006.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 2389aff94c8ea..6e568b8e98da0 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -367,10 +367,27 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs0854.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/expression-tree-restrictions" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1019.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1020.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1063.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" + + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1067.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/partial-types" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1501.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1704.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/assembly-references" @@ -1641,6 +1658,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0655.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0663.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs0674.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" @@ -1825,6 +1846,14 @@ "source_path_from_root": "/docs/csharp/misc/cs1632.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/lambda-expression-errors#syntax-limitations-in-lambda-expressions" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1534.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1535.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs1637.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/iterator-yield" @@ -1873,6 +1902,14 @@ "source_path_from_root": "/docs/csharp/misc/cs1925.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1928.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs1929.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs1939.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -1885,6 +1922,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1945.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/expression-tree-restrictions" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1950.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" + }, { "source_path_from_root": "/docs/csharp/misc/cs1951.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/expression-tree-restrictions" @@ -1901,6 +1942,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1954.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs3006.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs3016.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" diff --git a/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md b/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md index fe439f9db24dd..b46a9b91b4dcf 100644 --- a/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md +++ b/docs/csharp/language-reference/compiler-messages/array-declaration-errors.md @@ -15,11 +15,15 @@ f1_keywords: - "CS0820" - "CS0826" - "CS0846" + - "CS1062" + - "CS1063" + - "CS1064" - "CS1552" - "CS1586" - "CS1920" - "CS1921" - "CS1925" + - "CS1950" - "CS1954" - "CS3007" - "CS3016" @@ -49,9 +53,13 @@ helpviewer_keywords: - "CS0846" - "CS1552" - "CS1586" + - "CS1062" + - "CS1063" + - "CS1064" - "CS1920" - "CS1921" - "CS1925" + - "CS1950" - "CS1954" - "CS3007" - "CS3016" @@ -86,11 +94,13 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS0820**](#invalid-element-type): *Cannot assign array initializer to an implicitly typed local* - [**CS0826**](#invalid-element-type): *No best type found for implicitly typed array.* - [**CS0846**](#invalid-array-initializer): *A nested array initializer is expected* +- [**CS1063**](#invalid-collection-initializer): *The best overloaded Add method for the collection initializer element is obsolete.* - [**CS1552**](#invalid-array-rank): *Array type specifier, `[]`, must appear before parameter name* - [**CS1586**](#invalid-array-length): *Array creation must have array size or array initializer* - [**CS1920**](#invalid-collection-initializer): *Element initializer cannot be empty.* - [**CS1921**](#invalid-collection-initializer): *The best overloaded method match has wrong signature for the initializer element. The initializable `Add` must be an accessible instance method.* - [**CS1925**](#invalid-array-initializer): *Cannot initialize object of type 'type' with a collection initializer.* +- [**CS1950**](#invalid-collection-initializer): *The best overloaded Add method for the collection initializer has some invalid arguments.* - [**CS1954**](#invalid-collection-initializer): *The best overloaded method match for the collection initializer element cannot be used. Collection initializer '`Add`' methods cannot have `ref` or `out` parameters.* - [**CS9174**](#invalid-collection-initializer): *Cannot initialize type with a collection literal because the type is not constructible.* - [**CS9176**](#invalid-collection-initializer): *There is no target type for the collection literal.* @@ -103,6 +113,8 @@ That's by design. The text closely matches the text of the compiler error / warn In addition, the following warnings are covered in this article: +- [**CS1062**](#invalid-collection-initializer): *The best overloaded Add method for the collection initializer element is obsolete.* +- [**CS1064**](#invalid-collection-initializer): *The best overloaded Add method for the collection initializer element is obsolete.* - [**CS3007**](#common-language-specification-warnings): *Overloaded method 'method' differing only by unnamed array types is not CLS-compliant* - [**CS3016**](#common-language-specification-warnings): *Arrays as attribute arguments is not CLS-compliant* - [**CS0251**](#invalid-array-element-access): *Indexing an array with a negative index (array indices always start at zero)* @@ -120,8 +132,10 @@ You can learn more about arrays, collection initializers and collection expressi The following errors indicate that the code generated by the compiler for a collection initializer is invalid: - **CS0747**: *Invalid initializer member declarator.* +- **CS1063**: *The best overloaded Add method for the collection initializer element is obsolete.* - **CS1920**: *Element initializer cannot be empty.* - **CS1921**: *The best overloaded method match has wrong signature for the initializer element. The initializable `Add` must be an accessible instance method.* +- **CS1950**: *The best overloaded Add method for the collection initializer has some invalid arguments.* - **CS1954**: *The best overloaded method match for the collection initializer element cannot be used. Collection initializer '`Add`' methods cannot have `ref` or `out` parameters.* - **CS9174**: *Cannot initialize type with a collection literal because the type is not constructible.* - **CS9176**: *There is no target type for the collection literal.* @@ -130,6 +144,8 @@ The following errors indicate that the code generated by the compiler for a coll The compiler might also generate the following warning: +- **CS1062**: *The best overloaded Add method for the collection initializer element is obsolete.* +- **CS1064**: *The best overloaded Add method for the collection initializer element is obsolete.* - **CS9208**: *Collection expression may incur unexpected heap allocations. Consider explicitly creating an array, then converting to the final type to make the allocation explicit.* - **CS9209**: *Collection expression may incur unexpected heap allocations due to use of '`..`' spreads. Consider explicitly creating an array, then converting to the final type to make the allocation explicit.* @@ -137,6 +153,7 @@ The errors all indicate that the code generated by the compiler for a collection - A collection initializer contains a sequence of elements. You can't mix setting properties on the collection instance with adding elements in the same initializer. - A collection initializer that includes braces (`{` and `}`) can't be empty. +- The class must implement IEnumerable and have a public `Add` method. - A conforming `Add` method must be accessible and take one parameter that is the same type as the collection elements. The parameter can't include the `ref` or `out` modifier. - Overload resolution must pick one `Add` method as a better match. There can't be multiple matching methods that are equally good. - Collection expressions can initialize explicitly typed variables of a collection type. If the variable isn't a collection or array type, or is implicitly typed (using `var`), a collection initializer can't be used. diff --git a/docs/csharp/language-reference/compiler-messages/cs1019.md b/docs/csharp/language-reference/compiler-messages/cs1019.md deleted file mode 100644 index 4b619beb7e318..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1019.md +++ /dev/null @@ -1,65 +0,0 @@ ---- -description: "Compiler Error CS1019" -title: "Compiler Error CS1019" -ms.date: 07/20/2015 -f1_keywords: - - "CS1019" -helpviewer_keywords: - - "CS1019" -ms.assetid: 11a3acd8-bcab-4ead-a91b-a1498ea1eab5 ---- -# Compiler Error CS1019 - -Overloadable unary operator expected - - Something that looks like an overloaded unary operator has been declared, but the operator is missing or is in the wrong location in the signature. - - A *unary operator* is an operator that operates on a single operand. For example, `++` is a unary operator. You can overload some unary operators by using the `operator` keyword and specifying a single parameter of the type that the operator operates on. For example, if you want to overload the operator `++` for a user-defined class `Temperature` so that you can write `Temperature++`, you can define it in this way: - -```csharp -public static Temperature operator ++ (Temperature temp) -{ - temp.Degrees++; - return temp; -} -``` - - When you receive this error, you have declared something that looks like an overloaded unary operator, except that the operator itself is missing or is in the wrong location in the signature. If you remove the `++` from the signature in the previous example, you will generate CS1019. - - The following code generates CS1019: - -```csharp -// CS1019.cs -public class ii -{ - int i - { - get - { - return 0; - } - } -} - -public class a -{ - public int i; -// Generates CS1019: "ii" is not a unary operator. - public static a operator ii(a aa) - - // Use the following line instead: - //public static a operator ++(a aa) - { - aa.i++; - return aa; - } - - public static void Main() - { - } -} -``` - -## See also - -- [C# operators](../operators/index.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs1063.md b/docs/csharp/language-reference/compiler-messages/cs1063.md deleted file mode 100644 index cb31882ebb221..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1063.md +++ /dev/null @@ -1,73 +0,0 @@ ---- -description: "Compiler Error CS1063" -title: "Compiler Error CS1063" -ms.date: 9/17/2022 -f1_keywords: - - "CS1063" -helpviewer_keywords: - - "CS1063" ---- -# Compiler Error CS1063 - -The best overloaded `Add` method for the collection initializer element is obsolete. - -This error occurs when a type that implements `IEnumerable` implements one or more `Add` methods but the best matching overload is attributed with `ObsoleteAttribute` and the `ObsoleteAttribute.IsError` property is initialized to `true`. - -## Example - - The following sample generates CS1063: - -```csharp -// CS1063.cs (9,38) -using System; -using System.Collections; - -class Test -{ - public static void Main() - { - B coll = new B { "a" }; - } -} - -public class B : IEnumerable -{ - [Obsolete("Don't use this overload", true)] - public void Add(string s) - { - //... - } - public void Add(int i) - { - //... - } - IEnumerator IEnumerable.GetEnumerator() - { - return null; - } -} -``` - -## To correct this error - -If you own the enumerable type code and want to utilize a collection initializer, remove the `ObsoleteAttribute`. - -```csharp -public class B : IEnumerable -{ - public void Add(string s) - { - //... - } - public void Add(int i) - { - //... - } - IEnumerator IEnumerable.GetEnumerator() - { - return null; - } -} -``` - -If you do not own the enumerable type code, you will have to choose another means to initialize the collection. diff --git a/docs/csharp/language-reference/compiler-messages/cs1501.md b/docs/csharp/language-reference/compiler-messages/cs1501.md deleted file mode 100644 index 8deef7f0a399a..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1501.md +++ /dev/null @@ -1,61 +0,0 @@ ---- -description: "Compiler Error CS1501" -title: "Compiler Error CS1501" -ms.date: 07/20/2015 -f1_keywords: - - "CS1501" -helpviewer_keywords: - - "CS1501" -ms.assetid: 99a59646-e2c8-4ee5-9785-4a2c1ae77458 ---- -# Compiler Error CS1501 - -No overload for method 'method' takes 'number' arguments - - A call was made to a class method, but no definition of the method takes the specified number of arguments. - -## Example - - The following sample generates CS1501. - -```csharp -using System; - -namespace ConsoleApplication1 -{ - class Program - { - static void Main(string[] args) - { - ExampleClass ec = new ExampleClass(); - ec.ExampleMethod(); - ec.ExampleMethod(10); - // The following line causes compiler error CS1501 because - // ExampleClass does not contain an ExampleMethod that takes - // two arguments. - ec.ExampleMethod(10, 20); - } - } - - // ExampleClass contains two overloads for ExampleMethod. One of them - // has no parameters and one has a single parameter. - class ExampleClass - { - public void ExampleMethod() - { - Console.WriteLine("Zero parameters"); - } - - public void ExampleMethod(int i) - { - Console.WriteLine("One integer parameter."); - } - - //// To fix the error, you must add a method that takes two arguments. - //public void ExampleMethod (int i, int j) - //{ - // Console.WriteLine("Two integer parameters."); - //} - } -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index d59286f9bd26a..7087f9fc3c091 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -1,9 +1,30 @@ --- title: Fix errors that involve overload resolution description: Compiler errors and warnings that indicate a problem in your code related to overload resolution. Learn causes and fixes for these errors. +f1_keywords: + - "CS0663" + - "CS1019" + - "CS1020" + - "CS1062" + - "CS1064" + - "CS1501" + - "CS1534" + - "CS1535" + - "CS1928" + - "CS1929" + - "CS3006" - "CS9261" - "CS9262" helpviewer_keywords: + - "CS0663" + - "CS1019" + - "CS1020" + - "CS1501" + - "CS1534" + - "CS1535" + - "CS1928" + - "CS1929" + - "CS3006" - "CS9261" - "CS9262" ms.date: 09/10/2024 @@ -31,3 +52,351 @@ Your code violated the rules for using the - CS0022, CS0178, CS0248, CS0251, CS0270, CS0611, CS0623, CS0650, CS0719, CS0747, CS0820, CS0826, CS0846, CS1552, CS1586, - CS1920, CS1921, CS1925, CS1954, CS3007, CS3016, CS9174, CS9176, CS9185, CS9186, CS9187, CS9188, CS9203, CS9208, CS9209, - CS9210 + CS0022, CS0178, CS0248, CS0251, CS0270, CS0611, CS0623, CS0650, CS0719, CS0747, CS0820, CS0826, CS0846, CS1062, CS1063, + CS1064, CS1552, CS1586, CS1920, CS1921, CS1925, CS1950, CS1954, CS3007, CS3016, CS9174, CS9176, CS9185, CS9186, CS9187, + CS9188, CS9203, CS9208, CS9209, CS9210 - name: Inline arrays href: ./compiler-messages/inline-array-errors.md displayName: CS9164, CS9165, CS9166, CS9167, CS9168, CS9169, CS9172, CS9173, CS9180, CS9181, CS9182, CS9183, CS9184 - name: Lambda expressions href: ./compiler-messages/lambda-expression-errors.md displayName: > - CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098, + CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS3006, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098, CS9099, CS9100 - name: Overload resolution href: ./compiler-messages/overload-resolution-errors.md displayName: > 'overload resolution', - CS9261, CS9262 + CS0663, CS1019, CS1020, CS1501, CS1534, CS1535, CS1928, CS1929, CS9261, CS9262 - name: Restrictions on expression trees href: ./compiler-messages/expression-tree-restrictions.md displayName: > @@ -1209,8 +1209,6 @@ items: href: ../misc/cs0656.md - name: CS0662 href: ../misc/cs0662.md - - name: CS0663 - href: ../misc/cs0663.md - name: CS0664 href: ../misc/cs0664.md - name: CS0666 @@ -1393,10 +1391,6 @@ items: href: ../misc/cs1017.md - name: CS1018 href: ./compiler-messages/cs1018.md - - name: CS1019 - href: ./compiler-messages/cs1019.md - - name: CS1020 - href: ../misc/cs1020.md - name: CS1021 href: ../misc/cs1021.md - name: CS1022 @@ -1451,8 +1445,6 @@ items: href: ../misc/cs1059.md - name: CS1061 href: ./compiler-messages/cs1061.md - - name: CS1063 - href: ./compiler-messages/cs1063.md - name: CS1065 href: ./compiler-messages/cs1065.md - name: CS1100 @@ -1479,8 +1471,6 @@ items: href: ./compiler-messages/cs1112.md - name: CS1113 href: ../misc/cs1113.md - - name: CS1501 - href: ./compiler-messages/cs1501.md - name: CS1502 href: ./compiler-messages/cs1502.md - name: CS1503 @@ -1525,10 +1515,6 @@ items: href: ../misc/cs1528.md - name: CS1530 href: ../misc/cs1530.md - - name: CS1534 - href: ../misc/cs1534.md - - name: CS1535 - href: ../misc/cs1535.md - name: CS1536 href: ../misc/cs1536.md - name: CS1540 @@ -1775,10 +1761,6 @@ items: href: ../misc/cs1922.md - name: CS1926 href: ./compiler-messages/cs1926.md - - name: CS1928 - href: ../misc/cs1928.md - - name: CS1929 - href: ../misc/cs1929.md - name: CS1930 href: ../misc/cs1930.md - name: CS1931 @@ -1811,8 +1793,6 @@ items: href: ../misc/cs1948.md - name: CS1949 href: ../misc/cs1949.md - - name: CS1950 - href: ../misc/cs1950.md - name: CS1955 href: ../misc/cs1955.md - name: CS1958 @@ -2123,8 +2103,6 @@ items: href: ../misc/cs3004.md - name: CS3005 href: ../misc/cs3005.md - - name: CS3006 - href: ../misc/cs3006.md - name: CS3008 href: ../misc/cs3008.md - name: CS3009 diff --git a/docs/csharp/misc/cs0663.md b/docs/csharp/misc/cs0663.md deleted file mode 100644 index 0ce760f41dd82..0000000000000 --- a/docs/csharp/misc/cs0663.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS0663" -title: "Compiler Error CS0663" -ms.date: 07/20/2015 -f1_keywords: - - "CS0663" -helpviewer_keywords: - - "CS0663" -ms.assetid: 9f96c42b-dcc8-4a0f-8404-289fc88dba5e ---- -# Compiler Error CS0663 - -Cannot define overloaded methods that differ only on ref and out. - - Methods that differ only on their use of [in](../language-reference/keywords/method-parameters.md#in-parameter-modifier), [ref](../language-reference/keywords/ref.md) and [out](../language-reference/keywords/method-parameters.md#out-parameter-modifier) on a parameter are not allowed. - - The following sample generates CS0663: - -```csharp -// CS0663.cs -class TestClass -{ - public static void Main() - { - } - - public void Test(ref int i) - { - } - - public void Test(out int i) // CS0663 - { - } -} -``` diff --git a/docs/csharp/misc/cs1020.md b/docs/csharp/misc/cs1020.md deleted file mode 100644 index 7ce17acdbbe3c..0000000000000 --- a/docs/csharp/misc/cs1020.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -description: "Compiler Error CS1020" -title: "Compiler Error CS1020" -ms.date: 07/20/2015 -f1_keywords: - - "CS1020" -helpviewer_keywords: - - "CS1020" -ms.assetid: e8860769-a847-4248-a37b-77a59863467c ---- -# Compiler Error CS1020 - -Overloadable binary operator expected - - An attempt was made to define an operator overload, but the operator was not an overloadable binary operator, which takes two parameters. For the list of overloadable operators, see the [Overloadable operators](../language-reference/operators/operator-overloading.md#overloadable-operators) section of the [Operator overloading](../language-reference/operators/operator-overloading.md) article. - - The following sample generates CS1020: - -```csharp -// CS1020.cs -public class iii -{ - public static int operator ++(iii aa, int bb) // CS1020, change ++ to + - { - return 0; - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs1534.md b/docs/csharp/misc/cs1534.md deleted file mode 100644 index 88afd3dd56f43..0000000000000 --- a/docs/csharp/misc/cs1534.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: "Compiler Error CS1534" -title: "Compiler Error CS1534" -ms.date: 07/20/2015 -f1_keywords: - - "CS1534" -helpviewer_keywords: - - "CS1534" -ms.assetid: afb28c3a-a74c-4e47-b016-9e3245a5a1b1 ---- -# Compiler Error CS1534 - -Overloaded binary operator 'operator' takes two parameters - - The definition of a binary [operator](../language-reference/operators/operator-overloading.md) must take two parameters. - - The following sample generates CS1534: - -```csharp -// CS1534.cs -class MyClass -{ - public static MyClass operator - (MyClass MC1, MyClass MC2, MyClass MC3) // CS1534 - // try the following line instead - // public static MyClass operator - (MyClass MC1, MyClass MC2) - { - return new MyClass(); - } - - public static int Main() - { - return 1; - } -} -``` diff --git a/docs/csharp/misc/cs1535.md b/docs/csharp/misc/cs1535.md deleted file mode 100644 index 17e03ddae5f45..0000000000000 --- a/docs/csharp/misc/cs1535.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -description: "Compiler Error CS1535" -title: "Compiler Error CS1535" -ms.date: 07/20/2015 -f1_keywords: - - "CS1535" -helpviewer_keywords: - - "CS1535" -ms.assetid: 19f41e78-9aea-4575-abd0-60ddb927276f ---- -# Compiler Error CS1535 - -Overloaded unary operator 'operator' takes one parameter - - The definition of a unary [operator](../language-reference/operators/operator-overloading.md) must take one parameter. - -## Example - - The following sample generates CS1535: - -```csharp -// CS1535.cs -class MyClass -{ - // uncomment the method parameter to resolve CS1535 - public static MyClass operator ++ (/*MyClass MC1*/) // CS1535 - { - return new MyClass(); - } - - public static int Main() - { - return 1; - } -} -``` diff --git a/docs/csharp/misc/cs1928.md b/docs/csharp/misc/cs1928.md deleted file mode 100644 index 2571af0e04a54..0000000000000 --- a/docs/csharp/misc/cs1928.md +++ /dev/null @@ -1,47 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1928" -title: "Compiler Error CS1928" -ms.date: 07/20/2015 -f1_keywords: - - "CS1928" -helpviewer_keywords: - - "CS1928" -ms.assetid: bcc9dbef-ded5-4ddd-8c03-a9837cb6d165 ---- -# Compiler Error CS1928 - -'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments. - - This error is produced when the compiler cannot find a class member with the name of the method you have called. It can find an extension method with that name, but not with a signature that matches the types you passed in with your method call. - -## To correct this error - -1. Pass in types that match an existing extension method or class method. - -## Example - - The following code generates CS1928: - -```csharp -// cs1928.cs -class Test -{ - static void Main() - { - Test t = new Test(); - t.M("hi"); // CS1928 - } -} -static class Ext -{ - public static void M(this Test t, int i) - { - } -} -``` - - This error is often accompanied by CS1503: Argument 'n': cannot convert from 'typeA' to 'typeB'. - -## See also - -- [Extension Methods](../programming-guide/classes-and-structs/extension-methods.md) diff --git a/docs/csharp/misc/cs1929.md b/docs/csharp/misc/cs1929.md deleted file mode 100644 index 165742be43816..0000000000000 --- a/docs/csharp/misc/cs1929.md +++ /dev/null @@ -1,102 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1929" -title: "Compiler Error CS1929" -ms.date: 07/20/2015 -f1_keywords: - - "CS1929" -helpviewer_keywords: - - "CS1929" -ms.assetid: effdd5d4-e156-418b-9d45-4ca194ab4319 ---- -# Compiler Error CS1929 - -'typeB' does not contain a definition for 'method' and the best extension method overload 'typeC.method' requires a receiver of type 'typeA' - -This error is generated when you try to invoke an extension method from a class that it does not extend. In the example shown here, the extension method is defined for the derived class `D`, but not for the base class `B`. - -## To correct this error - -1. Create a new extension method for the type where you have to invoke it, or -2. move the call into an object of the type that the existing method extends. - -## Example - -The following code generates CS1929: - -```csharp -static class Extension -{ - public static void ExtensionMethod(this D d) - { - } -} - -class D : B -{ -} - -class B -{ - static void Main() - { - B b = new B(); - b.ExtensionMethod(); // CS1929 - } -} -``` - -The following code solves the CS1929 as described in 1. - by creating a new extension method for proper type 'B': - -```csharp -static class Extension -{ - public static void ExtensionMethod(this D d) - { - } - - public static void NewExtensionMethod(this B b) - { - } -} - -class D : B -{ -} - -class B -{ - static void Main() - { - B b = new B(); - b.NewExtensionMethod(); - } -} -``` - -The following code solves the CS1929 as described in 2. - moving the call into an object of the proper type 'D': - -```csharp -static class Extension -{ - public static void ExtensionMethod(this D d) - { - } -} - -class D : B -{ -} - -class B -{ - static void Main() - { - D d = new D(); - d.ExtensionMethod(); - } -} -``` - -## See also - -- [Extension Methods](../programming-guide/classes-and-structs/extension-methods.md) diff --git a/docs/csharp/misc/cs1950.md b/docs/csharp/misc/cs1950.md deleted file mode 100644 index 1905dd3162507..0000000000000 --- a/docs/csharp/misc/cs1950.md +++ /dev/null @@ -1,52 +0,0 @@ ---- -description: "Learn more about: Compiler Error CS1950" -title: "Compiler Error CS1950" -ms.date: 07/20/2015 -f1_keywords: - - "CS1950" -helpviewer_keywords: - - "CS1950" -ms.assetid: e37fb5b1-09e0-47a6-9db5-a48f90ea7bbb ---- -# Compiler Error CS1950 - -The best overloaded Add method 'name' for the collection initializer has some invalid arguments. - - To support collection initializers, a class must implement IEnumerable and have a public `Add` method. To initialize the type by using a collection initializer, the input parameter of the `Add` method must be compatible with the type of the object you are trying to add. - -## To correct this error - -- Use a compatible type in the collection initializer. - -- Modify the input parameter and/or accessibility of the `Add` method in the collection type. - -- Add a new `Add` method with an input parameter that matches what you are passing in. - -- Make your collection class generic so that it can have an `Add` method that accepts any type you pass in. - -## Example - - The following example generates CS1950: - -```csharp -// cs1950.cs -using System.Collections; -class TestClass : CollectionBase -{ - public void Add(int c) - { - } -} - -class Test -{ - static void Main() - { - TestClass t = new TestClass { "hi" }; // CS1950 - } -} -``` - -## See also - -- [Object and Collection Initializers](../programming-guide/classes-and-structs/object-and-collection-initializers.md) diff --git a/docs/csharp/misc/cs3006.md b/docs/csharp/misc/cs3006.md deleted file mode 100644 index b8561d554e48a..0000000000000 --- a/docs/csharp/misc/cs3006.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -description: "Learn more about: Compiler Warning (level 1) CS3006" -title: "Compiler Warning (level 1) CS3006" -ms.date: 07/20/2015 -f1_keywords: - - "CS3006" -helpviewer_keywords: - - "CS3006" -ms.assetid: efbfd898-e46f-4c3d-91e2-e2da0056b016 ---- -# Compiler Warning (level 1) CS3006 - -Overloaded method 'method' differing only in ref or out, or in array rank, is not CLS-compliant - - A method cannot be overloaded based on the [ref](../language-reference/keywords/ref.md) or [out](../language-reference/keywords/method-parameters.md#out-parameter-modifier) parameter and still comply with the Common Language Specification (CLS). For more information on CLS Compliance, see [Language independence and language-independent components](../../standard/language-independence.md). - -## Example - - The following example generates CS3006. To resolve this warning, comment out the assembly-level attribute or remove one of the method definitions. - -```csharp -// CS3006.cs - -using System; - -[assembly: CLSCompliant(true)] -public class MyClass -{ - public void f(int i) - { - } - - public void f(ref int i) // CS3006 - { - } - - 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 0a5bf149604a8..0c605d52864ea 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 @@ -11,8 +11,6 @@ f1_keywords: - "CS0847" - "CS0856" - "CS0857" - - "CS1062" - - "CS1064" - "CS1066" - "CS1072" - "CS1073" From cd86d192f5e42f2fee1100c93b7976f4f90fa918 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 10 Sep 2024 17:17:23 -0400 Subject: [PATCH 3/9] warnings --- .openpublishing.redirection.csharp.json | 8 ++++---- .../compiler-messages/overload-resolution.md | 2 +- docs/csharp/language-reference/toc.yml | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 6e568b8e98da0..b35dfcdcae1c9 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -371,10 +371,6 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1019.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" }, - { - "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1020.md", - "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" - }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1063.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -1770,6 +1766,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1016.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1020.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs1104.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index 7087f9fc3c091..92315559152f3 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -135,7 +135,7 @@ public class a Overloadable binary operator expected -An attempt was made to define an operator overload, but the operator was not an overloadable binary operator, which takes two parameters. For the list of overloadable operators, see the [Overloadable operators](../language-reference/operators/operator-overloading.md#overloadable-operators) section of the [Operator overloading](../language-reference/operators/operator-overloading.md) article. +An attempt was made to define an operator overload, but the operator was not an overloadable binary operator, which takes two parameters. For the list of overloadable operators, see the [Overloadable operators](../operators/operator-overloading.md#overloadable-operators) section of the [Operator overloading](../operators/operator-overloading.md) article. The following sample generates CS1020: diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index 8277c47ec04ca..ff68e65b570bb 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -510,7 +510,7 @@ items: CS0748, CS1621, CS1628, CS1632, CS1673, CS1686, CS1706, CS1989, CS3006, CS8030, CS8175, CS8916, CS8971, CS8972, CS8975, CS9098, CS9099, CS9100 - name: Overload resolution - href: ./compiler-messages/overload-resolution-errors.md + href: ./compiler-messages/overload-resolution.md displayName: > 'overload resolution', CS0663, CS1019, CS1020, CS1501, CS1534, CS1535, CS1928, CS1929, CS9261, CS9262 From f49381929c4569aa65d625cec0a838b90f3b7454 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 11 Sep 2024 14:36:18 -0400 Subject: [PATCH 4/9] Consolidate descriptions of overload issues Consolidate the description for the reason for different errors and the steps to fix those errors. --- .../compiler-messages/overload-resolution.md | 391 ++---------------- 1 file changed, 41 insertions(+), 350 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index 92315559152f3..172f9ac30ff6b 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -31,372 +31,63 @@ ms.date: 09/10/2024 --- # Resolve errors and warnings that impact overload resolution. -This article covers the following compiler warnings: - +This article covers the following compiler errors: + +- [**CS0663**](#incorrect-overload-definition) - *Cannot define overloaded methods that differ only on `ref` and `out`.* +- [**CS1019**](#incorrect-overload-definition) - *Overloadable unary operator expected* +- [**CS1020**](#incorrect-overload-definition) - *Overloadable binary operator expected* +- [**CS1501**](#no-overload-found) - *No overload for method 'method' takes 'number' arguments* +- [**CS1534**](#incorrect-overload-definition) - *Overloaded binary operator 'operator' takes two parameters* +- [**CS1535**](#incorrect-overload-definition) - *Overloaded unary operator 'operator' takes one parameter* +- [**CS1928**](#no-overload-found) - *'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments.* +- [**CS1929**](#no-overload-found) - *'TypeA' does not contain a definition for 'method' and the best extension method overload 'TypeB.method' requires a receiver of type 'TypeC'* - [**CS9261**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* - [**CS9262**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on this member.* -## Overload resolution priority - -- **CS9261** - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* -- **CS9262** - *Cannot use '`OverloadResolutionPriorityAttribute`' on this member.* - -Your code violated the rules for using the to favor one overload instead of another. You can't apply the `OverloadResolutionPriorityAttribute` to the following method types: - -- Non-indexer properties -- Property, indexer, or event accessors -- Conversion operators -- Lambdas -- Local functions -- Finalizers -- Static constructors - -In addition, you can't apply the `OverloadResolutionPriorityAttribute` to an `override` of a `virtual` or `abstract` member. The compiler uses the value from the base type declaration. - -## Compiler Error CS0663 - -Cannot define overloaded methods that differ only on ref and out. - -Methods that differ only on their use of [in](../keywords/method-parameters.md#in-parameter-modifier), [ref](../keywords/ref.md) and [out](../keywords/method-parameters.md#out-parameter-modifier) on a parameter are not allowed. - -The following sample generates CS0663: - -```csharp -// CS0663.cs -class TestClass -{ - public static void Main() - { - } - - public void Test(ref int i) - { - } - - public void Test(out int i) // CS0663 - { - } -} -``` - -## Compiler Error CS1019 - -Overloadable unary operator expected - -Something that looks like an overloaded unary operator has been declared, but the operator is missing or is in the wrong location in the signature. - -A *unary operator* is an operator that operates on a single operand. For example, `++` is a unary operator. You can overload some unary operators by using the `operator` keyword and specifying a single parameter of the type that the operator operates on. For example, if you want to overload the operator `++` for a user-defined class `Temperature` so that you can write `Temperature++`, you can define it in this way: - -```csharp -public static Temperature operator ++ (Temperature temp) -{ - temp.Degrees++; - return temp; -} -``` - -When you receive this error, you have declared something that looks like an overloaded unary operator, except that the operator itself is missing or is in the wrong location in the signature. If you remove the `++` from the signature in the previous example, you will generate CS1019. - -The following code generates CS1019: - -```csharp -// CS1019.cs -public class ii -{ - int i - { - get - { - return 0; - } - } -} - -public class a -{ - public int i; -// Generates CS1019: "ii" is not a unary operator. - public static a operator ii(a aa) - - // Use the following line instead: - //public static a operator ++(a aa) - { - aa.i++; - return aa; - } - - public static void Main() - { - } -} -``` - -## Compiler Error CS1020 - -Overloadable binary operator expected - -An attempt was made to define an operator overload, but the operator was not an overloadable binary operator, which takes two parameters. For the list of overloadable operators, see the [Overloadable operators](../operators/operator-overloading.md#overloadable-operators) section of the [Operator overloading](../operators/operator-overloading.md) article. - -The following sample generates CS1020: - -```csharp -// CS1020.cs -public class iii -{ - public static int operator ++(iii aa, int bb) // CS1020, change ++ to + - { - return 0; - } - - public static void Main() - { - } -} -``` - -## Compiler Error CS1501 - -No overload for method 'method' takes 'number' arguments - -A call was made to a class method, but no definition of the method takes the specified number of arguments. - -The following sample generates CS1501. - -```csharp -using System; +In addition, the following compiler warning: -namespace ConsoleApplication1 -{ - class Program - { - static void Main(string[] args) - { - ExampleClass ec = new ExampleClass(); - ec.ExampleMethod(); - ec.ExampleMethod(10); - // The following line causes compiler error CS1501 because - // ExampleClass does not contain an ExampleMethod that takes - // two arguments. - ec.ExampleMethod(10, 20); - } - } +- [**CS3006**](#incorrect-overload-definition) - *Overloaded method 'method' differing only in `ref` or `out`, or in array rank, is not CLS-compliant* - // ExampleClass contains two overloads for ExampleMethod. One of them - // has no parameters and one has a single parameter. - class ExampleClass - { - public void ExampleMethod() - { - Console.WriteLine("Zero parameters"); - } +## Incorrect overload definition - public void ExampleMethod(int i) - { - Console.WriteLine("One integer parameter."); - } +- **CS0663** - *Cannot define overloaded methods that differ only on `ref` and `out`.* +- **CS1019** - *Overloadable unary operator expected* +- **CS1020** - *Overloadable binary operator expected* +- **CS1534** - *Overloaded binary operator 'operator' takes two parameters* +- **CS1535** - *Overloaded unary operator 'operator' takes one parameter* - //// To fix the error, you must add a method that takes two arguments. - //public void ExampleMethod (int i, int j) - //{ - // Console.WriteLine("Two integer parameters."); - //} - } -} -``` +In addition, the following compiler warning: -## Compiler Error CS1534 +- **CS3006** - *Overloaded method 'method' differing only in `ref` or `out`, or in array rank, is not CLS-compliant* -Overloaded binary operator 'operator' takes two parameters +When you create overloaded operators in your class, the signature must match the number of parameters required for that operator. You have the wrong number of parameters in the operator definition. -The definition of a binary [operator](../operators/operator-overloading.md) must take two parameters. +In addition, overload operators must use the defined operator name. The only exception is when you create a [conversion operator](../operators/user-defined-conversion-operators.md), where the operator name matches the return type of the conversion. -The following sample generates CS1534: +## No overload found -```csharp -// CS1534.cs -class MyClass -{ - public static MyClass operator - (MyClass MC1, MyClass MC2, MyClass MC3) // CS1534 - // try the following line instead - // public static MyClass operator - (MyClass MC1, MyClass MC2) - { - return new MyClass(); - } +- [**CS1501**](#no-overload-found) - *No overload for method 'method' takes 'number' arguments* +- [**CS1928**](#no-overload-found) - *'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments.* +- [**CS1929**](#no-overload-found) - *'TypeA' does not contain a definition for 'method' and the best extension method overload 'TypeB.method' requires a receiver of type 'TypeC'* - public static int Main() - { - return 1; - } -} -``` +Your code calls a method where the name exists, but some arguments are incorrect, or you've used the wrong number of arguments. -## Compiler Error CS1535 +> [!NOTE] +> Error `CS1928` isn't used by the latest compilers. Newer compilers use `CS1929` exclusively. -Overloaded unary operator 'operator' takes one parameter - -The definition of a unary [operator](../operators/operator-overloading.md) must take one parameter. - -The following sample generates CS1535: - -```csharp -// CS1535.cs -class MyClass -{ - // uncomment the method parameter to resolve CS1535 - public static MyClass operator ++ (/*MyClass MC1*/) // CS1535 - { - return new MyClass(); - } - - public static int Main() - { - return 1; - } -} -``` - -## Compiler Error CS1928 - -Not used in Roslyn - -'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments. - -This error is produced when the compiler cannot find a class member with the name of the method you have called. It can find an extension method with that name, but not with a signature that matches the types you passed in with your method call. - -To correct this error, pass in types that match an existing extension method or class method. - -The following code generates CS1928: - -```csharp -// cs1928.cs -class Test -{ - static void Main() - { - Test t = new Test(); - t.M("hi"); // CS1928 - } -} -static class Ext -{ - public static void M(this Test t, int i) - { - } -} -``` - -This error is often accompanied by CS1503: Argument 'n': cannot convert from 'typeA' to 'typeB'. - -## Compiler Error CS1929 - -'typeB' does not contain a definition for 'method' and the best extension method overload 'typeC.method' requires a receiver of type 'typeA' - -This error is generated when you try to invoke an extension method from a class that it does not extend. In the example shown here, the extension method is defined for the derived class `D`, but not for the base class `B`. - -To correct this error, create a new extension method for the type where you have to invoke it, or move the call into an object of the type that the existing method extends. - -The following code generates CS1929: - -```csharp -static class Extension -{ - public static void ExtensionMethod(this D d) - { - } -} - -class D : B -{ -} - -class B -{ - static void Main() - { - B b = new B(); - b.ExtensionMethod(); // CS1929 - } -} -``` - -The following code solves the CS1929 as described in 1. - by creating a new extension method for proper type 'B': - -```csharp -static class Extension -{ - public static void ExtensionMethod(this D d) - { - } - - public static void NewExtensionMethod(this B b) - { - } -} - -class D : B -{ -} - -class B -{ - static void Main() - { - B b = new B(); - b.NewExtensionMethod(); - } -} -``` - -The following code solves the CS1929 as described in 2. - moving the call into an object of the proper type 'D': - -```csharp -static class Extension -{ - public static void ExtensionMethod(this D d) - { - } -} - -class D : B -{ -} - -class B -{ - static void Main() - { - D d = new D(); - d.ExtensionMethod(); - } -} -``` - -## Compiler Warning (level 1) CS3006 - -Overloaded method 'method' differing only in ref or out, or in array rank, is not CLS-compliant - -A method cannot be overloaded based on the [ref](../keywords/ref.md) or [out](../keywords/method-parameters.md#out-parameter-modifier) parameter and still comply with the Common Language Specification (CLS). For more information on CLS Compliance, see [Language independence and language-independent components](../../../standard/language-independence.md). - -The following example generates CS3006. To resolve this warning, comment out the assembly-level attribute or remove one of the method definitions. - -```csharp -// CS3006.cs +## Overload resolution priority -using System; +- **CS9261** - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* +- **CS9262** - *Cannot use '`OverloadResolutionPriorityAttribute`' on this member.* -[assembly: CLSCompliant(true)] -public class MyClass -{ - public void f(int i) - { - } +Your code violated the rules for using the to favor one overload instead of another. You can't apply the `OverloadResolutionPriorityAttribute` to the following method types: - public void f(ref int i) // CS3006 - { - } +- Non-indexer properties +- Property, indexer, or event accessors +- Conversion operators +- Lambdas +- Local functions +- Finalizers +- Static constructors - public static void Main() - { - } -} -``` +In addition, you can't apply the `OverloadResolutionPriorityAttribute` to an `override` of a `virtual` or `abstract` member. The compiler uses the value from the base type declaration. From ca5b547574254cca1f9ecb0e9ae95ff5e6f5a014 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 11 Sep 2024 17:19:10 -0400 Subject: [PATCH 5/9] Add ambiguity errors types --- .openpublishing.redirection.csharp.json | 24 ++ .../compiler-messages/cs0034.md | 107 ------ .../compiler-messages/overload-resolution.md | 333 ++++++++++++++++++ docs/csharp/language-reference/toc.yml | 15 +- docs/csharp/misc/cs0035.md | 55 --- docs/csharp/misc/cs0111.md | 30 -- docs/csharp/misc/cs0121.md | 86 ----- docs/csharp/misc/cs0457.md | 61 ---- docs/csharp/misc/cs1007.md | 39 -- ...n-t-have-specifics-on-this-csharp-error.md | 1 - 10 files changed, 359 insertions(+), 392 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/cs0034.md delete mode 100644 docs/csharp/misc/cs0035.md delete mode 100644 docs/csharp/misc/cs0111.md delete mode 100644 docs/csharp/misc/cs0121.md delete mode 100644 docs/csharp/misc/cs0457.md delete mode 100644 docs/csharp/misc/cs1007.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index b35dfcdcae1c9..e77151c721d41 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -307,6 +307,10 @@ "source_path_from_root": "/docs/csharp/language-reference/builtin-types/nint-nuint.md", "redirect_url": "/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#native-sized-integers" }, + { + "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/cs0178.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -1482,10 +1486,22 @@ "source_path_from_root": "/docs/csharp/misc/cs0022.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "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/cs0105.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0111.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0121.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs0138.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" @@ -1578,6 +1594,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/cs0457.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs0466.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" @@ -1774,6 +1794,10 @@ "source_path_from_root": "/docs/csharp/misc/cs1104.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/params-arrays" }, + { + "source_path_from_root": "/docs/csharp/misc/cs1007.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/overload-resolution" + }, { "source_path_from_root": "/docs/csharp/misc/cs1510.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" diff --git a/docs/csharp/language-reference/compiler-messages/cs0034.md b/docs/csharp/language-reference/compiler-messages/cs0034.md deleted file mode 100644 index 6910449381888..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs0034.md +++ /dev/null @@ -1,107 +0,0 @@ ---- -description: "Compiler Error CS0034" -title: "Compiler Error CS0034" -ms.date: 07/20/2015 -f1_keywords: - - "CS0034" -helpviewer_keywords: - - "CS0034" -ms.assetid: cb72ec9b-3a6b-40f3-b2ad-2ea122920edd ---- -# Compiler Error CS0034 - -Operator 'operator' is ambiguous on operands of type 'type1' and 'type2' - - An operator was used on two objects and the compiler found more than one conversion. Because conversions have to be unique, you either have to make a cast or to make one of the conversions explicit. For more information, see [User-defined conversion operators](../operators/user-defined-conversion-operators.md). - - The following sample generates CS0034: - -```csharp -// CS0034.cs -public class A -{ - // Allows for the conversion of A object to int. - public static implicit operator int (A s) - { - return 0; - } - - public static implicit operator string (A i) - { - return null; - } -} - -public class B -{ - public static implicit operator int (B s) - // One way to resolve this CS0034 is to make one conversion explicit. - // public static explicit operator int (B s) - { - return 0; - } - - public static implicit operator string (B i) - { - return null; - } - - public static implicit operator B (string i) - { - return null; - } - - public static implicit operator B (int i) - { - return null; - } -} - -public class C -{ - public static void Main() - { - A a = new A(); - B b = new B(); - b = b + a; // CS0034 - // Another way to resolve this CS0034 is to make a cast. - // b = b + (int)a; - } -} -``` - - In C# 1.1, a compiler bug made it possible to define a class that has implicit user-defined conversions to both `int` and `bool`, and to use a bitwise `AND` operator (`&`) on objects of that type. In C# 2.0, this bug was fixed to bring the compiler into compliance with the C# specification, and therefore the following code will now cause CS0034: - -```csharp -namespace CS0034 -{ - class TestClass2 - { - public void Test() - { - TestClass o1 = new TestClass(); - TestClass o2 = new TestClass(); - TestClass o3 = o1 & o2; //CS0034 - } - } - - class TestClass - { - public static implicit operator int(TestClass o) - { - return 1; - } - - public static implicit operator TestClass(int v) - { - return new TestClass(); - } - - public static implicit operator bool(TestClass o) - { - return true; - } - } - -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index 172f9ac30ff6b..785157bc6c201 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -2,6 +2,12 @@ title: Fix errors that involve overload resolution description: Compiler errors and warnings that indicate a problem in your code related to overload resolution. Learn causes and fixes for these errors. f1_keywords: + - "CS0034" + - "CS0035" + - "CS0111" + - "CS0121" + - "CS0457" + - "CS1007" - "CS0663" - "CS1019" - "CS1020" @@ -13,9 +19,16 @@ f1_keywords: - "CS1928" - "CS1929" - "CS3006" + - "CS8315" - "CS9261" - "CS9262" helpviewer_keywords: + - "CS0034" + - "CS0035" + - "CS0111" + - "CS0121" + - "CS0457" + - "CS1007" - "CS0663" - "CS1019" - "CS1020" @@ -25,6 +38,7 @@ helpviewer_keywords: - "CS1928" - "CS1929" - "CS3006" + - "CS8315" - "CS9261" - "CS9262" ms.date: 09/10/2024 @@ -41,6 +55,7 @@ This article covers the following compiler errors: - [**CS1535**](#incorrect-overload-definition) - *Overloaded unary operator 'operator' takes one parameter* - [**CS1928**](#no-overload-found) - *'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments.* - [**CS1929**](#no-overload-found) - *'TypeA' does not contain a definition for 'method' and the best extension method overload 'TypeB.method' requires a receiver of type 'TypeC'* +- [**CS8315**](#) - *Operator '{0}' is ambiguous on operands '{1}' and '{2}'* - [**CS9261**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* - [**CS9262**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on this member.* @@ -91,3 +106,321 @@ Your code violated the rules for using the (T x); + delegate T D1(U u); + + static void F(D1 d1) { ol_invoked = 1; } + static void F(D1 d1t) { ol_invoked = 2; } + static void F(D1 d1t) { ol_invoked = 3; } + + static int Test001() + { + F(delegate(int x) { return 1; }); // CS0121 + if (ol_invoked == 1) + return 0; + else + return 1; + } + + static int Main() + { + return Test001(); + } +} +``` + +## Compiler Error CS0457 + +Ambiguous user defined conversions 'Conversion method name 1' and 'Conversion method name 2' when converting from 'type name 1' to 'type name 2' + +Two conversion methods are applicable, and the compiler is unable to decide which one to use. + +One scenario that can cause this error is as follows: + +- You want to convert class A to class B where A and B are unrelated. +- A is derived from A0, and there is a method that converts from A0 to B. +- B has a subclass B1 and there is a method that converts from A to B1. + +The compiler will weight the two conversion methods equally, because the first conversion provides the best destination type, and the second conversion provides the best source type. Since the compiler will be unable to choose, this error will be generated. To resolve, write a new explicit method converting A to B. + +Another scenario that causes this error is if there are two methods that convert A to B. To fix, specify which conversion to use through an explicit cast. + +The following sample generates CS0457. + +```csharp +// CS0457.cs +using System; +public class A { } + +public class G0 { } +public class G1 : G0 { } + +public class H0 { + public static implicit operator G0(H0 h) { + return new G0(); + } +} +public class H1 : H0 { + public static implicit operator G1(H1 h) { + return new G1(); + } +} + +public class Test +{ + public static void F0(G0 g) { } + public static void Main() + { + H1 h1a = new H1(); + F0(h1a); // CS0457 + } +} +``` diff --git a/docs/csharp/language-reference/toc.yml b/docs/csharp/language-reference/toc.yml index ff68e65b570bb..f4c2d35a5e70e 100644 --- a/docs/csharp/language-reference/toc.yml +++ b/docs/csharp/language-reference/toc.yml @@ -513,7 +513,8 @@ items: href: ./compiler-messages/overload-resolution.md displayName: > 'overload resolution', - CS0663, CS1019, CS1020, CS1501, CS1534, CS1535, CS1928, CS1929, CS9261, CS9262 + CS0034, CS0035, CS0111, CS0121, CS0457, CS1007, CS0663, CS1019, CS1020, CS1501, CS1534, CS1535, CS1928, CS1929, + CS8315, CS9261, CS9262 - name: Restrictions on expression trees href: ./compiler-messages/expression-tree-restrictions.md displayName: > @@ -593,10 +594,6 @@ items: href: ../misc/cs0030.md - name: CS0031 href: ../misc/cs0031.md - - name: CS0034 - href: ./compiler-messages/cs0034.md - - name: CS0035 - href: ../misc/cs0035.md - name: CS0036 href: ../misc/cs0036.md - name: CS0037 @@ -685,8 +682,6 @@ items: href: ../misc/cs0107.md - name: CS0110 href: ../misc/cs0110.md - - name: CS0111 - href: ../misc/cs0111.md - name: CS0112 href: ../misc/cs0112.md - name: CS0113 @@ -703,8 +698,6 @@ items: href: ../misc/cs0119.md - name: CS0120 href: ./compiler-messages/cs0120.md - - name: CS0121 - href: ../misc/cs0121.md - name: CS0122 href: ./compiler-messages/cs0122.md - name: CS0123 @@ -1005,8 +998,6 @@ items: href: ../misc/cs0455.md - name: CS0456 href: ../misc/cs0456.md - - name: CS0457 - href: ../misc/cs0457.md - name: CS0459 href: ../misc/cs0459.md - name: CS0460 @@ -1369,8 +1360,6 @@ items: href: ../misc/cs1003.md - name: CS1004 href: ../misc/cs1004.md - - name: CS1007 - href: ../misc/cs1007.md - name: CS1008 href: ../misc/cs1008.md - name: CS1009 diff --git a/docs/csharp/misc/cs0035.md b/docs/csharp/misc/cs0035.md deleted file mode 100644 index 0ac12c731f80f..0000000000000 --- a/docs/csharp/misc/cs0035.md +++ /dev/null @@ -1,55 +0,0 @@ ---- -description: "Compiler Error CS0035" -title: "Compiler Error CS0035" -ms.date: 07/20/2015 -f1_keywords: - - "CS0035" -helpviewer_keywords: - - "CS0035" -ms.assetid: a622113e-98a4-4583-992c-1fb55139320a ---- -# Compiler Error CS0035 - -Operator 'operator' is ambiguous on an operand of type 'type' - -The compiler has more than one available conversion and does not know which to choose before applying the operator. - -The following sample generates CS0035: - -```csharp -// CS0035.cs -class MyClass -{ - private int i; - - public MyClass(int i) - { - this.i = i; - } - - public static implicit operator double(MyClass x) - { - return (double) x.i; - } - - public static implicit operator decimal(MyClass x) - { - return (decimal) x.i; - } -} - -class MyClass2 -{ - static void Main() - { - MyClass x = new MyClass(7); - object o = - x; // CS0035 - // try a cast: - // object o = - (double)x; - } -} -``` - -## See also - -- [User-defined conversion operators](../language-reference/operators/user-defined-conversion-operators.md) diff --git a/docs/csharp/misc/cs0111.md b/docs/csharp/misc/cs0111.md deleted file mode 100644 index f25d9d59f48c9..0000000000000 --- a/docs/csharp/misc/cs0111.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -description: "Compiler Error CS0111" -title: "Compiler Error CS0111" -ms.date: 07/20/2015 -f1_keywords: - - "CS0111" -helpviewer_keywords: - - "CS0111" -ms.assetid: 87afb689-f27b-451d-84eb-d6bdf17aea41 ---- -# Compiler Error CS0111 - -Type 'class' already defines a member called 'member' with the same parameter types - - CS0111 occurs if a class contains two member declarations with the same name and parameter types. For more information, see [Methods](../programming-guide/classes-and-structs/methods.md). - -## Example - - The following sample generates CS0111. - -```csharp -// CS0111.cs -class A -{ - void Test() { } - public static void Test(){} // CS0111 - - public static void Main() {} -} -``` diff --git a/docs/csharp/misc/cs0121.md b/docs/csharp/misc/cs0121.md deleted file mode 100644 index efaf62646ec7d..0000000000000 --- a/docs/csharp/misc/cs0121.md +++ /dev/null @@ -1,86 +0,0 @@ ---- -description: "Compiler Error CS0121" -title: "Compiler Error CS0121" -ms.date: 08/14/2018 -f1_keywords: - - "CS0121" -helpviewer_keywords: - - "CS0121" -ms.assetid: 316cb77e-a500-4129-ae1b-e68b9188fd3e ---- -# Compiler error CS0121 - -The call is ambiguous between the following methods or properties: 'method1' and 'method2' - -Due to implicit conversion, the compiler was not able to call one form of an overloaded method. You can resolve this error in one of the following ways: - -- Specify the method parameters in such a way that implicit conversion does not take place. -- Remove all overloads for the method. -- Cast to proper type before calling the method. -- Use named arguments. - -## Example 1 - -The following examples generate compiler error CS0121: - -```csharp -public class Program -{ - static void f(int i, double d) - { - } - - static void f(double d, int i) - { - } - - public static void Main() - { - f(1, 1); // CS0121 - - // Try the following code instead: - // f(1, 1.0); - // or - // f(1.0, 1); - // or - // f(1, (double)1); // Cast and specify which method to call. - // or - // f(i: 1, 1); - // or - // f(d: 1, 1); - - // f(i: 1, d: 1); // This still gives CS0121 - } -} -``` - -## Example 2 - -```csharp -class Program2 -{ - static int ol_invoked = 0; - - delegate int D1(int x); - delegate T D1(T x); - delegate T D1(U u); - - static void F(D1 d1) { ol_invoked = 1; } - static void F(D1 d1t) { ol_invoked = 2; } - static void F(D1 d1t) { ol_invoked = 3; } - - static int Test001() - { - F(delegate(int x) { return 1; }); // CS0121 - if (ol_invoked == 1) - return 0; - else - return 1; - } - - static int Main() - { - return Test001(); - } -} -``` diff --git a/docs/csharp/misc/cs0457.md b/docs/csharp/misc/cs0457.md deleted file mode 100644 index c10cec2cb27f8..0000000000000 --- a/docs/csharp/misc/cs0457.md +++ /dev/null @@ -1,61 +0,0 @@ ---- -description: "Compiler Error CS0457" -title: "Compiler Error CS0457" -ms.date: 07/20/2015 -f1_keywords: - - "CS0457" -helpviewer_keywords: - - "CS0457" -ms.assetid: 5d5cf762-c817-4468-9455-59e966b8c140 ---- -# Compiler Error CS0457 - -Ambiguous user defined conversions 'Conversion method name 1' and 'Conversion method name 2' when converting from 'type name 1' to 'type name 2' - - Two conversion methods are applicable, and the compiler is unable to decide which one to use. - - One scenario that can cause this error is as follows: - -- You want to convert class A to class B where A and B are unrelated. - -- A is derived from A0, and there is a method that converts from A0 to B. - -- B has a subclass B1 and there is a method that converts from A to B1. - - The compiler will weight the two conversion methods equally, because the first conversion provides the best destination type, and the second conversion provides the best source type. Since the compiler will be unable to choose, this error will be generated. To resolve, write a new explicit method converting A to B. - - Another scenario that causes this error is if there are two methods that convert A to B. To fix, specify which conversion to use through an explicit cast. - -## Example - - The following sample generates CS0457. - -```csharp -// CS0457.cs -using System; -public class A { } - -public class G0 { } -public class G1 : G0 { } - -public class H0 { - public static implicit operator G0(H0 h) { - return new G0(); - } -} -public class H1 : H0 { - public static implicit operator G1(H1 h) { - return new G1(); - } -} - -public class Test -{ - public static void F0(G0 g) { } - public static void Main() - { - H1 h1a = new H1(); - F0(h1a); // CS0457 - } -} -``` diff --git a/docs/csharp/misc/cs1007.md b/docs/csharp/misc/cs1007.md deleted file mode 100644 index ab61cb176048a..0000000000000 --- a/docs/csharp/misc/cs1007.md +++ /dev/null @@ -1,39 +0,0 @@ ---- -description: "Compiler Error CS1007" -title: "Compiler Error CS1007" -ms.date: 07/20/2015 -f1_keywords: - - "CS1007" -helpviewer_keywords: - - "CS1007" -ms.assetid: b56ee2c6-8e79-4b9b-8c59-194bdb22bc3e ---- -# Compiler Error CS1007 - -Property accessor already defined - - When you declare a [property](../programming-guide/classes-and-structs/using-properties.md), you must also declare its accessor methods. However, a property cannot have more than one `get` accessor method or more than one `set` accessor method. - -## Example - - The following sample generates CS1007: - -```csharp -// CS1007.cs -public class clx -{ - public int MyProperty - { - get - { - return 0; - } - get // CS1007, this is the second get method - { - return 0; - } - } - - 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 0c605d52864ea..b10a6f9dbb1ea 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 @@ -291,7 +291,6 @@ f1_keywords: - "CS8309" - "CS8310" - "CS8312" - - "CS8315" - "CS8321" - "CS8322" - "CS8323" From 4ba965502e80f10d3536ccfe63d8a71a24d96633 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 11 Sep 2024 17:24:17 -0400 Subject: [PATCH 6/9] warnings --- .../compiler-messages/overload-resolution.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index 785157bc6c201..228adba13fd41 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -111,7 +111,7 @@ In addition, you can't apply the `OverloadResolutionPriorityAttribute` to an `ov Type 'class' already defines a member called 'member' with the same parameter types -CS0111 occurs if a class contains two member declarations with the same name and parameter types. For more information, see [Methods](../programming-guide/classes-and-structs/methods.md). +CS0111 occurs if a class contains two member declarations with the same name and parameter types. For more information, see [Methods](../../programming-guide/classes-and-structs/methods.md). The following sample generates CS0111. @@ -130,7 +130,7 @@ class A Property accessor already defined -When you declare a [property](../programming-guide/classes-and-structs/using-properties.md), you must also declare its accessor methods. However, a property cannot have more than one `get` accessor method or more than one `set` accessor method. +When you declare a [property](../../programming-guide/classes-and-structs/using-properties.md), you must also declare its accessor methods. However, a property cannot have more than one `get` accessor method or more than one `set` accessor method. The following sample generates CS1007: @@ -302,7 +302,7 @@ class MyClass2 } ``` -# Compiler error CS0121 +## Compiler error CS0121 The call is ambiguous between the following methods or properties: 'method1' and 'method2' From 83a2659be339156ecfb7f177cddf234532e02c1e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 12 Sep 2024 16:30:48 -0400 Subject: [PATCH 7/9] Final edits --- .../compiler-messages/overload-resolution.md | 371 +++--------------- 1 file changed, 47 insertions(+), 324 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index 228adba13fd41..4ec66a3c48727 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -43,11 +43,17 @@ helpviewer_keywords: - "CS9262" ms.date: 09/10/2024 --- -# Resolve errors and warnings that impact overload resolution. +# Resolve errors and warnings that affect overload resolution. This article covers the following compiler errors: +- [**CS0034**](#ambiguous-overloads) - *Operator is ambiguous on operands of type '`type1`' and '`type2`'* +- [**CS0035**](#ambiguous-overloads) - *Operator is ambiguous on an operand of type 'type'* +- [**CS0111**](#duplicate-overloads-defined) - *Type already defines a member called 'name' with the same parameter types* +- [**CS0121**](#ambiguous-overloads) - *The call is ambiguous between the following methods or properties* +- [**CS0457**](#ambiguous-overloads) - *Ambiguous user defined conversions when converting from 'type1' to 'type2'* - [**CS0663**](#incorrect-overload-definition) - *Cannot define overloaded methods that differ only on `ref` and `out`.* +- [**CS1007**](#duplicate-overloads-defined) - *Property accessor already defined* - [**CS1019**](#incorrect-overload-definition) - *Overloadable unary operator expected* - [**CS1020**](#incorrect-overload-definition) - *Overloadable binary operator expected* - [**CS1501**](#no-overload-found) - *No overload for method 'method' takes 'number' arguments* @@ -55,7 +61,7 @@ This article covers the following compiler errors: - [**CS1535**](#incorrect-overload-definition) - *Overloaded unary operator 'operator' takes one parameter* - [**CS1928**](#no-overload-found) - *'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments.* - [**CS1929**](#no-overload-found) - *'TypeA' does not contain a definition for 'method' and the best extension method overload 'TypeB.method' requires a receiver of type 'TypeC'* -- [**CS8315**](#) - *Operator '{0}' is ambiguous on operands '{1}' and '{2}'* +- [**CS8315**](#ambiguous-overloads) - *Operator is ambiguous on operands* - [**CS9261**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* - [**CS9262**](#overload-resolution-priority) - *Cannot use '`OverloadResolutionPriorityAttribute`' on this member.* @@ -79,17 +85,52 @@ When you create overloaded operators in your class, the signature must match the In addition, overload operators must use the defined operator name. The only exception is when you create a [conversion operator](../operators/user-defined-conversion-operators.md), where the operator name matches the return type of the conversion. +## Duplicate overloads defined + +- **CS0111** - *Type already defines a member called 'name' with the same parameter types* +- **CS1007** - *Property accessor already defined* + +These errors indicate that you created overloads that aren't different. Common reasons include: + +- The matching parameter types have an identity conversion. For example `M(dynamic)` and `M(object)`, or `M2(string) and `M2(string?)`. +- The matching parameter types differ only in reference modifier. For example, `M(ref int)` and `M(out int)`. + ## No overload found -- [**CS1501**](#no-overload-found) - *No overload for method 'method' takes 'number' arguments* -- [**CS1928**](#no-overload-found) - *'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments.* -- [**CS1929**](#no-overload-found) - *'TypeA' does not contain a definition for 'method' and the best extension method overload 'TypeB.method' requires a receiver of type 'TypeC'* +- **CS1501** - *No overload for method 'method' takes 'number' arguments* +- **CS1928** - *'Type' does not contain a definition for 'method' and the best extension method overload 'method' has some invalid arguments.* +- **CS1929** - *'TypeA' does not contain a definition for 'method' and the best extension method overload 'TypeB.method' requires a receiver of type 'TypeC'* -Your code calls a method where the name exists, but some arguments are incorrect, or you've used the wrong number of arguments. +Your code calls a method where the name exists, but some arguments are incorrect, or you've used the wrong number of arguments. If you're trying to call an extension method, the containing class might be in a namespace that's not imported. > [!NOTE] > Error `CS1928` isn't used by the latest compilers. Newer compilers use `CS1929` exclusively. +## Ambiguous overloads + +- **CS0034** - *Operator is ambiguous on operands of type '`type1`' and '`type2`'* +- **CS0035** - *Operator is ambiguous on an operand of type 'type'* +- **CS0121** - *The call is ambiguous between the following methods or properties* +- **CS0457** - *Ambiguous user defined conversions when converting from 'type1' to 'type2'* +- **CS8315** - *Operator is ambiguous on operands* + +These errors indicate there isn't one better overload than others. There are several possible causes for these errors: + +- Different arguments are better for each parameter. One might be an exact match and the other requires a conversion: + + ```csharp + void M(int a, double b) { } + void M(double a, int b) {} + + M(1, 1); // ambiguous + ``` + +- Multiple user defined conversion operators are candidates. Conversion operators can be declared in the source type, its base types, the destination type, or its base types. If a conversion operator was defined in more than one, the compiler can't pick. +- Multiple conversion operators could perform a compatible conversion. +- One or more arguments uses `default` (not `default(T)`) and multiple types could be chosen. + +In most of these cases, adding an explicit cast can specify which overload should be chosen. + ## Overload resolution priority - **CS9261** - *Cannot use '`OverloadResolutionPriorityAttribute`' on an overriding member.* @@ -106,321 +147,3 @@ Your code violated the rules for using the (T x); - delegate T D1(U u); - - static void F(D1 d1) { ol_invoked = 1; } - static void F(D1 d1t) { ol_invoked = 2; } - static void F(D1 d1t) { ol_invoked = 3; } - - static int Test001() - { - F(delegate(int x) { return 1; }); // CS0121 - if (ol_invoked == 1) - return 0; - else - return 1; - } - - static int Main() - { - return Test001(); - } -} -``` - -## Compiler Error CS0457 - -Ambiguous user defined conversions 'Conversion method name 1' and 'Conversion method name 2' when converting from 'type name 1' to 'type name 2' - -Two conversion methods are applicable, and the compiler is unable to decide which one to use. - -One scenario that can cause this error is as follows: - -- You want to convert class A to class B where A and B are unrelated. -- A is derived from A0, and there is a method that converts from A0 to B. -- B has a subclass B1 and there is a method that converts from A to B1. - -The compiler will weight the two conversion methods equally, because the first conversion provides the best destination type, and the second conversion provides the best source type. Since the compiler will be unable to choose, this error will be generated. To resolve, write a new explicit method converting A to B. - -Another scenario that causes this error is if there are two methods that convert A to B. To fix, specify which conversion to use through an explicit cast. - -The following sample generates CS0457. - -```csharp -// CS0457.cs -using System; -public class A { } - -public class G0 { } -public class G1 : G0 { } - -public class H0 { - public static implicit operator G0(H0 h) { - return new G0(); - } -} -public class H1 : H0 { - public static implicit operator G1(H1 h) { - return new G1(); - } -} - -public class Test -{ - public static void F0(G0 g) { } - public static void Main() - { - H1 h1a = new H1(); - F0(h1a); // CS0457 - } -} -``` From 6adbdc7667c282b4c0e9f7d5875d46820e7684ec Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 12 Sep 2024 16:38:39 -0400 Subject: [PATCH 8/9] Fix related errors --- .../compiler-messages/overload-resolution.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index 4ec66a3c48727..b07ec601c64d0 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -7,8 +7,8 @@ f1_keywords: - "CS0111" - "CS0121" - "CS0457" - - "CS1007" - "CS0663" + - "CS1007" - "CS1019" - "CS1020" - "CS1062" @@ -28,8 +28,8 @@ helpviewer_keywords: - "CS0111" - "CS0121" - "CS0457" - - "CS1007" - "CS0663" + - "CS1007" - "CS1019" - "CS1020" - "CS1501" @@ -94,6 +94,7 @@ These errors indicate that you created overloads that aren't different. Common r - The matching parameter types have an identity conversion. For example `M(dynamic)` and `M(object)`, or `M2(string) and `M2(string?)`. - The matching parameter types differ only in reference modifier. For example, `M(ref int)` and `M(out int)`. +- You've included both an `init` and a `set` accessor for a property. ## No overload found From 5865480dd05640066b9c760e65f55ee54b6a12ad Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 12 Sep 2024 16:42:55 -0400 Subject: [PATCH 9/9] typo --- .../language-reference/compiler-messages/overload-resolution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/compiler-messages/overload-resolution.md b/docs/csharp/language-reference/compiler-messages/overload-resolution.md index b07ec601c64d0..14165c107d0af 100644 --- a/docs/csharp/language-reference/compiler-messages/overload-resolution.md +++ b/docs/csharp/language-reference/compiler-messages/overload-resolution.md @@ -92,7 +92,7 @@ In addition, overload operators must use the defined operator name. The only exc These errors indicate that you created overloads that aren't different. Common reasons include: -- The matching parameter types have an identity conversion. For example `M(dynamic)` and `M(object)`, or `M2(string) and `M2(string?)`. +- The matching parameter types have an identity conversion. For example `M(dynamic)` and `M(object)`, or `M2(string)` and `M2(string?)`. - The matching parameter types differ only in reference modifier. For example, `M(ref int)` and `M(out int)`. - You've included both an `init` and a `set` accessor for a property.