From 5e3a054973f703c7097f5046ef828df37dffc769 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 20 Dec 2023 08:40:26 -0500 Subject: [PATCH] Document compiler errors and warnings for method parameter / argument mismatch (#38797) * create stub for CS7036 * build warning * stash this. This may be the wrong list of items to add to this page. * finish consolidating articles * fix build warning * edit text * grammar check * updated redirected links * fix one link * Apply suggestions from code review Co-authored-by: Tom Dykstra --------- Co-authored-by: Tom Dykstra --- .openpublishing.redirection.csharp.json | 52 +++++ .../compiler-messages/constructor-errors.md | 6 + .../compiler-messages/cs1739.md | 44 ----- .../compiler-messages/cs1740.md | 40 ---- .../compiler-messages/cs1742.md | 42 ---- .../compiler-messages/cs8964.md | 40 ---- .../parameter-argument-mismatch.md | 187 ++++++++++++++++++ .../compiler-messages/ref-modifiers-errors.md | 5 + .../compiler-messages/toc.yml | 31 +-- docs/csharp/misc/cs0182.md | 50 ----- docs/csharp/misc/cs0591.md | 36 ---- docs/csharp/misc/cs0599.md | 15 -- docs/csharp/misc/cs0617.md | 45 ----- docs/csharp/misc/cs0633.md | 53 ----- docs/csharp/misc/cs0643.md | 49 ----- docs/csharp/misc/cs0655.md | 41 ---- docs/csharp/misc/cs0839.md | 46 ----- docs/csharp/misc/cs1016.md | 46 ----- ...n-t-have-specifics-on-this-csharp-error.md | 18 +- docs/whats-new/dotnet-docs-mod0.md | 2 +- 20 files changed, 257 insertions(+), 591 deletions(-) delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1739.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1740.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs1742.md delete mode 100644 docs/csharp/language-reference/compiler-messages/cs8964.md create mode 100644 docs/csharp/language-reference/compiler-messages/parameter-argument-mismatch.md delete mode 100644 docs/csharp/misc/cs0182.md delete mode 100644 docs/csharp/misc/cs0591.md delete mode 100644 docs/csharp/misc/cs0599.md delete mode 100644 docs/csharp/misc/cs0617.md delete mode 100644 docs/csharp/misc/cs0633.md delete mode 100644 docs/csharp/misc/cs0643.md delete mode 100644 docs/csharp/misc/cs0655.md delete mode 100644 docs/csharp/misc/cs0839.md delete mode 100644 docs/csharp/misc/cs1016.md diff --git a/.openpublishing.redirection.csharp.json b/.openpublishing.redirection.csharp.json index 6aa56c88dfbd9..38ab121a3702b 100644 --- a/.openpublishing.redirection.csharp.json +++ b/.openpublishing.redirection.csharp.json @@ -347,10 +347,22 @@ "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/cs1739.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1740.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1741.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1742.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs1921.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -419,6 +431,10 @@ "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8892.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/warning-waves#cs8892" }, + { + "source_path_from_root": "/docs/csharp/language-reference/compiler-messages/cs8964.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, { "source_path_from_root": "/docs/csharp/language-reference/compiler-options/addmodule-compiler-option.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-options/inputs" @@ -1332,6 +1348,10 @@ "source_path_from_root": "/docs/csharp/misc/cs0171.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/feature-version-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0182.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, { "source_path_from_root": "/docs/csharp/misc/cs0192.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" @@ -1400,10 +1420,22 @@ "source_path_from_root": "/docs/csharp/misc/cs0576.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0591.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0599.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, { "source_path_from_root": "/docs/csharp/misc/cs0611.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0617.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, { "source_path_from_root": "/docs/csharp/misc/cs0623.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/array-declaration-errors" @@ -1412,6 +1444,18 @@ "source_path_from_root": "/docs/csharp/misc/cs0631.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0633.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, + { + "source_path_from_root": "/docs/csharp/misc/cs0643.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, + { + "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/cs0687.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/using-directive-errors" @@ -1460,6 +1504,14 @@ "source_path_from_root": "/docs/csharp/misc/cs0838.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/expression-tree-restrictions" }, + { + "source_path_from_root": "/docs/csharp/misc/cs0839.md", + "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/parameter-argument-mismatch" + }, + { + "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/cs1510.md", "redirect_url": "/dotnet/csharp/language-reference/compiler-messages/ref-modifiers-errors" diff --git a/docs/csharp/language-reference/compiler-messages/constructor-errors.md b/docs/csharp/language-reference/compiler-messages/constructor-errors.md index 6e72cf6e3b011..84db10d4301e4 100644 --- a/docs/csharp/language-reference/compiler-messages/constructor-errors.md +++ b/docs/csharp/language-reference/compiler-messages/constructor-errors.md @@ -15,6 +15,7 @@ f1_keywords: - "CS8054" # ERR_EnumsCantContainDefaultConstructor - "CS8091" # ERR_ExternHasConstructorInitializer - "CS8358" # ERR_AttributeCtorInParameter + - "CS8861" - "CS8862" # ERR_UnexpectedOrMissingConstructorInitializerInRecord - "CS8867" # ERR_NoCopyConstructorInBaseType - "CS8868" # ERR_CopyConstructorMustInvokeBaseCopyConstructor @@ -58,6 +59,7 @@ helpviewer_keywords: - "CS8054" - "CS8091" - "CS8358" + - "CS8861" - "CS8862" - "CS8867" - "CS8868" @@ -107,6 +109,7 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS0768**](#constructor-calls-with-base-and-this): *Constructor cannot call itself through another constructor.* - [**CS8054**](#constructor-declaration): *Enums cannot contain explicit parameterless constructors.* - [**CS8091**](#constructor-declaration): *cannot be extern and have a constructor initializer.* +- [**CS8861**](#primary-constructor-declaration): *Unexpected argument list.* - [**CS8862**](#primary-constructor-declaration): *A constructor declared in a type with parameter list must have 'this' constructor initializer.* - [**CS8358**](#constructor-declaration): *Cannot use attribute constructor because it has 'in' parameters.* - [**CS8867**](#records-and-copy-constructors): *No accessible copy constructor found in base type '{0}'.* @@ -234,6 +237,7 @@ Adding the `record` modifier to a `struct` or `class` type creates a [record](.. The compiler emits the following errors when a primary constructor violates one or more rules on primary constructors for classes and structs: +- **CS8861**: *Unexpected argument list.* - **CS8862**: *A constructor declared in a type with parameter list must have 'this' constructor initializer.* - **CS9105**: *Cannot use primary constructor parameter in this context.* - **CS9106**: *Identifier is ambiguous between type and parameter in this context.* @@ -275,6 +279,8 @@ Readonly only struct types have the following extra restrictions on primary cons In all these cases, the restrictions on primary constructor parameters are consistent with restrictions on data fields in those types. The restrictions are because a primary constructor parameter may be transformed into a synthesized field in the type. Therefore primary constructor parameters must follow the rules that apply to that synthesized field. +A derived primary constructor calls the base primary constructor by supplying the parameters to the base constructor. You must use the parameter names from the derived constructor declaration. + The warnings provide guidance on captured or shadowed primary constructor parameters. - **CS9107**: *Parameter is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.* This warning indicates that your code may be allocated two copies of a primary constructor parameter. Because the parameter is passed to the base class, the base class likely uses it. Because the derived class accesses it, it may have a second copy of the same parameter. That extra storage may not be intended. diff --git a/docs/csharp/language-reference/compiler-messages/cs1739.md b/docs/csharp/language-reference/compiler-messages/cs1739.md deleted file mode 100644 index aa9cd182b1113..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1739.md +++ /dev/null @@ -1,44 +0,0 @@ ---- -description: "Compiler Error CS1739" -title: "Compiler Error CS1739" -ms.date: 9/17/2022 -f1_keywords: - - "CS1739" -helpviewer_keywords: - - "CS1739" ---- -# Compiler Error CS1739 - -The best overload for does not have a parameter named - -## Example - - The following sample generates CS1739: - -```csharp -// CS1739.cs (11,31) -using System; - -public class A -{ - public int this[Range range] => 42; -} -public class C -{ - public static void Main() - { - Console.Write(new A()[param: 1..^1]); - } -} -``` - -## To correct this error - -Use the name of the parameter as it is declared in the member to correct this error. - -```csharp - public static void Main() - { - Console.Write(new A()[range: 1..^1]); - } -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs1740.md b/docs/csharp/language-reference/compiler-messages/cs1740.md deleted file mode 100644 index e463d6325464a..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1740.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS1740" -title: "Compiler Error CS1740" -ms.date: 9/17/2022 -f1_keywords: - - "CS1740" -helpviewer_keywords: - - "CS1740" ---- -# Compiler Error CS1740 - -Named argument cannot be specified multiple times - -## Example - - The following sample generates CS1740: - -The compiler does not support passing more than one value for a named argument. - -```csharp -// CS1740.cs (9,17) -class C -{ - static void M(params int[] x) - { - } - static void Main() - { - M(x: 1, x: 2); - } -} -``` - -## To correct this error - -Pick which value should be passed as the argument and remove the other: - -```csharp - M(x: 1); -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs1742.md b/docs/csharp/language-reference/compiler-messages/cs1742.md deleted file mode 100644 index 23f43d9f7b176..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs1742.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -description: "Compiler Error CS1742" -title: "Compiler Error CS1742" -ms.date: 9/18/2022 -f1_keywords: - - "CS1742" -helpviewer_keywords: - - "CS1742" ---- -# Compiler Error CS1742 - -An array access may not have a named argument specifier - -## Example - - The following sample generates CS1742: - -```csharp -// CS1742.cs (0,0) -public class B -{ - static void Main() - { - int[] arr = { }; - int s = arr[arr: 1]; - } -} -``` - -An array may not be declared with a named argument. This code generates CS1742 because using the name `arr` to refer to an argument when accessing the array is not syntactically correct. - -## To correct this error - -Remove the use of named arguments when accessing an array to correct this error: - -```csharp - static void Main() - { - int[] arr = { }; - int s = arr[1]; - } -``` diff --git a/docs/csharp/language-reference/compiler-messages/cs8964.md b/docs/csharp/language-reference/compiler-messages/cs8964.md deleted file mode 100644 index 2a85276222844..0000000000000 --- a/docs/csharp/language-reference/compiler-messages/cs8964.md +++ /dev/null @@ -1,40 +0,0 @@ ---- -description: "Compiler Error CS8964" -title: "Compiler Error CS8964" -ms.date: 9/28/2023 -f1_keywords: - - "CS8964" -helpviewer_keywords: - - "CS8964" ---- -# Compiler Error CS8964 - -The `CallerArgumentExpressionAttribute` may only be applied to parameters with default values. - -## Example - - The following sample generates CS8964: - -```csharp -// CS8964.cs (6,44) - -using System.Runtime.CompilerServices; - -class C -{ - public void Predicate(bool condition, [CallerArgumentExpression("condition")] string conditionExpr) {} -} -``` - -## To correct this error - -Add a default value to the affected parameters: - -```csharp -using System.Runtime.CompilerServices; - -class C -{ - public void Predicate(bool condition, [CallerArgumentExpression("condition")] string? conditionExpr = null) {} -} -``` diff --git a/docs/csharp/language-reference/compiler-messages/parameter-argument-mismatch.md b/docs/csharp/language-reference/compiler-messages/parameter-argument-mismatch.md new file mode 100644 index 0000000000000..dff8a9973aec7 --- /dev/null +++ b/docs/csharp/language-reference/compiler-messages/parameter-argument-mismatch.md @@ -0,0 +1,187 @@ +--- +title: Errors and warnings for parameter / argument mismatches +description: These errors and warnings are issued when arguments are missing or arguments can't be used for one or more parameters on a method. Learn how to diagnose and fix them. +f1_keywords: + - "CS0182" + - "CS0591" + - "CS0599" + - "CS0617" + - "CS0633" + - "CS0643" + - "CS0655" + - "CS0839" + - "CS1016" + - "CS1739" + - "CS1740" + - "CS1742" + - "CS1744" + - "CS1746" + - "CS7036" + - "CS7067" + - "CS8324" + - "CS8905" + - "CS8943" + - "CS8944" + - "CS8945" + - "CS8948" + - "CS8949" + - "CS8950" + - "CS8951" + - "CS8964" + - "CS8965" + - "CS8966" +helpviewer_keywords: + - "CS0182" + - "CS0591" + - "CS0599" + - "CS0617" + - "CS0633" + - "CS0643" + - "CS0655" + - "CS0839" + - "CS1016" + - "CS1739" + - "CS1740" + - "CS1742" + - "CS1744" + - "CS1746" + - "CS7036" + - "CS7067" + - "CS8324" + - "CS8905" + - "CS8943" + - "CS8944" + - "CS8945" + - "CS8948" + - "CS8949" + - "CS8950" + - "CS8951" + - "CS8964" + - "CS8965" + - "CS8966" +ms.date: 12/19/2023 +--- +# Parameter and argument mismatch + +The compiler generates the following errors when there's no argument supplied for a formal parameter, or the argument isn't valid for that parameter: + + +- [**CS0182**](#arguments-for-attributes): *An attribute argument must be a constant expression, `typeof` expression or array creation expression of an attribute parameter type* +- [**CS0591**](#arguments-for-attributes): *Invalid value for argument to attribute* +- [**CS0599**](#arguments-for-attributes): *Invalid value for named attribute argument 'argument'* +- [**CS0617**](#arguments-for-attributes): *Not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static.* +- [**CS0633**](#arguments-for-attributes): *The argument to the attribute must be a valid identifier* +- [**CS0643**](#arguments-for-attributes): *Duplicate named attribute argument* +- [**CS0655**](#arguments-for-attributes): *Not a valid named attribute argument because it is not a valid attribute parameter type* +- [**CS0839**](#missing-argument): *Argument missing.* +- [**CS1016**](#named-and-optional-parameters-and-arguments): *Named attribute argument expected* +- [**CS1739**](#named-and-optional-parameters-and-arguments): *The best overload for does not have a parameter named* +- [**CS1740**](#named-and-optional-parameters-and-arguments): *Named argument cannot be specified multiple times* +- [**CS1742**](#named-and-optional-parameters-and-arguments): *An array access may not have a named argument specifier* +- [**CS1744**](#named-and-optional-parameters-and-arguments): *Named argument specifies a parameter for which a positional argument has already been given* +- [**CS1746**](#named-and-optional-parameters-and-arguments): *The delegate does not have a parameter named 'name'* +- [**CS7036**](#missing-argument): *There is no argument given that corresponds to the required parameter* +- [**CS7067**](#named-and-optional-parameters-and-arguments): *Attribute constructor parameter is optional, but no default parameter value was specified.* +- [**CS8324**](#named-and-optional-parameters-and-arguments): *Named argument specifications must appear after all fixed arguments have been specified in a dynamic invocation.* +- [**CS8905**](#named-and-optional-parameters-and-arguments): *A function pointer cannot be called with named arguments.* +- [**CS8943**](#interpolated-string-handler): *null is not a valid parameter name. To get access to the receiver of an instance method, use the empty string as the parameter name.* +- [**CS8944**](#interpolated-string-handler): *Method is not an instance method, the receiver cannot be an interpolated string handler argument.* +- [**CS8945**](#interpolated-string-handler): *Not a valid parameter name.* +- [**CS8948**](#interpolated-string-handler): *`InterpolatedStringHandlerArgumentAttribute` arguments cannot refer to the parameter the attribute is used on.* +- [**CS8949**](#interpolated-string-handler): *The `InterpolatedStringHandlerArgumentAttribute` applied to parameter is malformed and cannot be interpreted. Construct an instance of it manually.* +- [**CS8950**](#interpolated-string-handler): *Parameter is an argument to the interpolated string handler conversion on parameter, but the corresponding argument is specified after the interpolated string expression. Reorder the arguments.* +- [**CS8951**](#interpolated-string-handler): *Parameter is not explicitly provided, but is used as an argument to the interpolated string handler conversion on parameter.* +- [**CS8964**](#caller-debugging-information): *The `CallerArgumentExpressionAttribute` may only be applied to parameters with default values* +- [**CS8965**](#caller-debugging-information): *The `CallerArgumentExpressionAttribute` applied to parameter will have no effect because it's self-referential.* +- [**CS8966**](#caller-debugging-information): *The `CallerArgumentExpressionAttribute` will have no effect because it applies to a member that is used in contexts that do not allow optional arguments* + +## Missing argument + +The following general errors are issued when the compiler can't match arguments to all member parameters: + +- **CS0839**: *Argument missing.* +- **CS7036**: *There is no argument given that corresponds to the required parameter* + +These errors are general: The compiler can't match the arguments given in a method call to the required parameters of the method. Check the following causes: + +- Make sure you included all necessary arguments. +- Make sure the arguments are in the correct order. +- Make sure all arguments are the correct type. +- Make sure overload resolution rules chose the method you expected. + +You might also see *CS7036* if you wrote overloaded local functions. Local functions can't be overloaded. The compiler only recognizes the first local function with that name. Check if you meant to call a different local function. + +These errors often appear with other diagnostics that can help diagnose the correct cause. + +## Arguments for attributes + +The compiler issues these errors when an argument to an attribute constructor is incorrect: + +- **CS0182**: *An attribute argument must be a constant expression, `typeof` expression or array creation expression of an attribute parameter type* +- **CS0591**: *Invalid value for argument to attribute* +- **CS0599**: *Invalid value for named attribute argument 'argument'* +- **CS0617**: *Not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static.* +- **CS0633**: *The argument to the attribute must be a valid identifier* +- **CS0643**: *Duplicate named attribute argument* +- **CS0655**: *not a valid named attribute argument because it is not a valid attribute parameter type* + +If you use the on your attribute definition, make sure the allowed values aren't mutually exclusive. Check that the type and order of arguments to the attribute are correct. Make sure the text of string arguments is valid. For many attributes, the argument must be a valid C# identifier. Arguments to attribute constructors must be compile-time constants. Therefore, they're limited to types that support literal constants. In addition, the following types that allow literal constants are disallowed as attribute parameters: + +- [sbyte](../../language-reference/builtin-types/integral-numeric-types.md) +- [ushort](../../language-reference/builtin-types/integral-numeric-types.md) +- [uint](../../language-reference/builtin-types/integral-numeric-types.md) +- [ulong](../../language-reference/builtin-types/integral-numeric-types.md) +- [decimal](../../language-reference/builtin-types/floating-point-numeric-types.md) + +You can't specify repeated named arguments with the same parameter name. You can only set accessible properties when you initialize an attribute. You can't set private properties. + +## Named and optional parameters and arguments + +The compiler issues the following errors for incorrect use of named and optional arguments: + +- **CS1016**: *Named attribute argument expected* +- **CS1739**: *The best overload for does not have a parameter named* +- **CS1740**: *Named argument cannot be specified multiple times* +- **CS1742**: *An array access may not have a named argument specifier* +- **CS1744**: *Named argument specifies a parameter for which a positional argument has already been given* +- **CS1746**: *The delegate does not have a parameter named 'name'* +- **CS7067**: *Attribute constructor parameter is optional, but no default parameter value was specified.* +- **CS8324**: *Named argument specifications must appear after all fixed arguments have been specified in a dynamic invocation.* +- **CS8905**: *A function pointer cannot be called with named arguments.* + +Check for the following causes of these errors: + +- The parameter name of the named argument is incorrect. +- The chosen overload doesn't have a parameter matching the named argument. +- A parameter name is repeated on more than one argument. +- A positional (unnamed) argument appears after named arguments. +- Named arguments aren't allowed for array index parameters. + +## Interpolated string handler + +The compiler issues the following errors when you specified an [interpolated string handler](../tokens/interpolated.md#compilation-of-interpolated-strings) incorrectly. + +- **CS8943**: *null is not a valid parameter name. To get access to the receiver of an instance method, use the empty string as the parameter name.* +- **CS8944**: *Not an instance method, the receiver cannot be an interpolated string handler argument.* +- **CS8945**: *Not a valid parameter name.* +- **CS8948**: *`InterpolatedStringHandlerArgumentAttribute` arguments cannot refer to the parameter the attribute is used on.* +- **CS8949**: *The `InterpolatedStringHandlerArgumentAttribute` applied to parameter is malformed and cannot be interpreted. Construct an instance manually.* +- **CS8950**: *Parameter is an argument to the interpolated string handler conversion on parameter, but the corresponding argument is specified after the interpolated string expression.* +- **CS8951**: *Parameter is not explicitly provided, but is used as an argument to the interpolated string handler conversion on parameter.* + +An interpolated string handler is a pattern-based construct. It's important to get the pattern correct. Consult the [feature spec](~/_csharplang/proposals/csharp-10.0/improved-interpolated-strings.md#the-handler-pattern), or follow the tutorial on [building an interpolated string handler](../../whats-new/tutorials/interpolated-string-handler.md). + +## Caller debugging information + +The compiler issues the following error on an incorrect use of the : + +- **CS8964**: *The `CallerArgumentExpressionAttribute` may only be applied to parameters with default values* + +In addition, the compiler issues the following warnings on an incorrect use of the `CallerArgumentExpressionAttribute`: + +- **CS8965**: *The `CallerArgumentExpressionAttribute` applied to parameter will have no effect because it's self-referential.* +- **CS8966**: *The `CallerArgumentExpressionAttribute` will have no effect because it applies to a member that is used in contexts that do not allow optional arguments* + +Any parameter annotated with the `CallerArgumentExpression` attribute must have a default value. diff --git a/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md b/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md index a8faba9e8f628..f4790ec42da50 100644 --- a/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md +++ b/docs/csharp/language-reference/compiler-messages/ref-modifiers-errors.md @@ -22,6 +22,7 @@ f1_keywords: - "CS8167" - "CS8168" - "CS8169" + - "CS8196" - "CS8325" - "CS8326" - "CS8327" @@ -87,6 +88,7 @@ helpviewer_keywords: - "CS8167" - "CS8168" - "CS8169" + - "CS8196" - "CS8325" - "CS8326" - "CS8327" @@ -160,6 +162,7 @@ That's by design. The text closely matches the text of the compiler error / warn - [**CS8167**](#ref-safety-violations): *Cannot return by reference a member of parameter because it is not a `ref` or `out` parameter* - [**CS8168**](#ref-safety-violations): *Cannot return local by reference because it is not a ref local* - [**CS8169**](#ref-safety-violations): *Cannot return a member of local variable by reference because it is not a ref local* +- [**CS8196**](#reference-variable-restrictions): *Reference to an implicitly-typed out variable is not permitted in the same argument list.* - [**CS8325**](#reference-variable-restrictions): *'`await`' cannot be used in an expression containing a `ref` conditional operator* - [**CS8326**](#reference-variable-restrictions): *Both conditional operator values must be ref values or neither may be a ref value* - [**CS8327**](#reference-variable-restrictions): *The expression must be of correct type to match the alternative ref value* @@ -243,6 +246,7 @@ The following errors indicate that a reference variable can't be used where you - **CS1939**: *Cannot pass the range variable as an `out` or `ref` parameter* - **CS1988**: *Async methods cannot have `ref`, `in` or `out` parameters* - **CS7084**: *A Windows Runtime event may not be passed as an `out` or `ref` parameter.* +- **CS8196**: *Reference to an implicitly-typed `out` variable is not permitted in the same argument list.* - **CS8325**: *'await' cannot be used in an expression containing a `ref` conditional operator* - **CS8326**: *Both conditional operator values must be ref values or neither may be a ref value* - **CS8327**: *The expression must be of correct type to match the alternative ref value* @@ -268,6 +272,7 @@ To fix the error, remove the reference variable where it isn't allowed: - Remove the `ref` modifier from the first parameter of a [extension method](../../programming-guide/classes-and-structs/extension-methods.md) where that type isn't a value type or a generic type constrained as a value type. - Either both or neither [conditional operator expressions] must be `ref` variables. Either remove `ref` from one expression, or add it to the other. If it's a `ref` conditional expression, both expressions must be the same type. - `ref` and `out` parameters can't have [default values](../../programming-guide/classes-and-structs/named-and-optional-arguments.md). Either remove the `ref` or `out` modifier, or remove the default value. +- An implicitly typed `out` variable declaration can't also appear elsewhere in the same argument list. - You can't put reference variables in a `using` statement in `async` methods lambda expressions. - The range variable in a [LINQ query expression](../../linq/get-started/query-expression-basics.md) can't be passed by reference. - You can't deconstruct an object into reference variables. Replace the reference variables with value variables. diff --git a/docs/csharp/language-reference/compiler-messages/toc.yml b/docs/csharp/language-reference/compiler-messages/toc.yml index f6a85c5fd2099..f0912220db671 100644 --- a/docs/csharp/language-reference/compiler-messages/toc.yml +++ b/docs/csharp/language-reference/compiler-messages/toc.yml @@ -15,6 +15,11 @@ 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: Parameter / argument mismatch + href: parameter-argument-mismatch.md + displayName: > + CS0182, CS0591, CS0599, CS0617, CS0633, CS0643, CS0655, CS0839, CS1016, CS1739, CS1740, CS1742, CS1744, CS1746, CS7036, + CS7067, CS8196, CS8324, CS8861, CS8905, CS8943, CS8944, CS8945, CS8948, CS8949, CS8950, CS8951, CS8964, CS8965, CS8966 - name: Reference parameters href: ref-modifiers-errors.md displayName: > @@ -326,8 +331,6 @@ items: href: ../../misc/cs0180.md - name: CS0181 href: cs0181.md - - name: CS0182 - href: ../../misc/cs0182.md - name: CS0185 href: ../../misc/cs0185.md - name: CS0186 @@ -724,16 +727,12 @@ items: href: ../../misc/cs0589.md - name: CS0590 href: ../../misc/cs0590.md - - name: CS0591 - href: ../../misc/cs0591.md - name: CS0592 href: cs0592.md - name: CS0594 href: ../../misc/cs0594.md - name: CS0596 href: ../../misc/cs0596.md - - name: CS0599 - href: ../../misc/cs0599.md - name: CS0601 href: ../../misc/cs0601.md - name: CS0609 @@ -742,8 +741,6 @@ items: href: ../../misc/cs0610.md - name: CS0616 href: cs0616.md - - name: CS0617 - href: ../../misc/cs0617.md - name: CS0619 href: ../../misc/cs0619.md - name: CS0620 @@ -756,8 +753,6 @@ items: href: ../../misc/cs0625.md - name: CS0629 href: ../../misc/cs0629.md - - name: CS0633 - href: ../../misc/cs0633.md - name: CS0635 href: ../../misc/cs0635.md - name: CS0636 @@ -766,8 +761,6 @@ items: href: ../../misc/cs0637.md - name: CS0641 href: ../../misc/cs0641.md - - name: CS0643 - href: ../../misc/cs0643.md - name: CS0644 href: ../../misc/cs0644.md - name: CS0645 @@ -780,8 +773,6 @@ items: href: ../../misc/cs0648.md - name: CS0653 href: ../../misc/cs0653.md - - name: CS0655 - href: ../../misc/cs0655.md - name: CS0656 href: ../../misc/cs0656.md - name: CS0662 @@ -958,8 +949,6 @@ items: href: ../../misc/cs0836.md - name: CS0837 href: ../../misc/cs0837.md - - name: CS0839 - href: ../../misc/cs0839.md - name: CS0840 href: cs0840.md - name: CS0841 @@ -994,8 +983,6 @@ items: href: ../../misc/cs1014.md - name: CS1015 href: ../../misc/cs1015.md - - name: CS1016 - href: ../../misc/cs1016.md - name: CS1017 href: ../../misc/cs1017.md - name: CS1018 @@ -1374,12 +1361,6 @@ items: href: cs1736.md - name: CS1737 href: cs1737.md - - name: CS1739 - href: cs1739.md - - name: CS1740 - href: cs1740.md - - name: CS1742 - href: cs1742.md - name: CS1750 href: cs1750.md - name: CS1751 @@ -1630,8 +1611,6 @@ items: href: cs8515.md - name: CS8817 href: CS8817.md - - name: CS8964 - href: CS8964.md - name: CS9043 href: cs9043.md - name: CS9050 diff --git a/docs/csharp/misc/cs0182.md b/docs/csharp/misc/cs0182.md deleted file mode 100644 index 6ad21962308c1..0000000000000 --- a/docs/csharp/misc/cs0182.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -description: "Compiler Error CS0182" -title: "Compiler Error CS0182" -ms.date: 07/20/2015 -f1_keywords: - - "CS0182" -helpviewer_keywords: - - "CS0182" -ms.assetid: a9e97bb8-f06e-499f-aadf-26abc2082f98 ---- -# Compiler Error CS0182 - -An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type - -Certain restrictions apply to what kinds of arguments may be used with attributes. Note that in addition to the restrictions specified in the error message, the following types are NOT allowed as attribute arguments: - -- [sbyte](../language-reference/builtin-types/integral-numeric-types.md) - -- [ushort](../language-reference/builtin-types/integral-numeric-types.md) - -- [uint](../language-reference/builtin-types/integral-numeric-types.md) - -- [ulong](../language-reference/builtin-types/integral-numeric-types.md) - -- [decimal](../language-reference/builtin-types/floating-point-numeric-types.md) - -For more information, see [Attributes](/dotnet/csharp/advanced-topics/reflection-and-attributes). - -## Example - - The following sample generates CS0182: - -```csharp -// CS0182.cs -public class MyClass -{ - static string s = "Test"; - - [System.Diagnostics.ConditionalAttribute(s)] // CS0182 - // try the following line instead - // [System.Diagnostics.ConditionalAttribute("Test")] - void NonConstantArgumentToConditional() - { - } - - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0591.md b/docs/csharp/misc/cs0591.md deleted file mode 100644 index 4326b7082c455..0000000000000 --- a/docs/csharp/misc/cs0591.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -description: "Compiler Error CS0591" -title: "Compiler Error CS0591" -ms.date: 07/20/2015 -f1_keywords: - - "CS0591" -helpviewer_keywords: - - "CS0591" -ms.assetid: b8acbcdb-dc66-4338-9ddd-d606e5a2c57e ---- -# Compiler Error CS0591 - -Invalid value for argument to 'attribute' attribute - - An attribute was passed either an invalid argument or two mutually exclusive arguments. - -## Example - - The following sample generates CS0591: - -```csharp -// CS0591.cs -using System; - -[AttributeUsage(0)] // CS0591 -class I: Attribute -{ -} - -public class a -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0599.md b/docs/csharp/misc/cs0599.md deleted file mode 100644 index c6274bb193521..0000000000000 --- a/docs/csharp/misc/cs0599.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -description: "Compiler Error CS0599" -title: "Compiler Error CS0599" -ms.date: 07/20/2015 -f1_keywords: - - "CS0599" -helpviewer_keywords: - - "CS0599" -ms.assetid: d1f5f179-8f7f-43e0-ac87-b5204f0e443b ---- -# Compiler Error CS0599 - -Invalid value for named attribute argument 'argument' - - An invalid argument was passed to an attribute. diff --git a/docs/csharp/misc/cs0617.md b/docs/csharp/misc/cs0617.md deleted file mode 100644 index f45c14f584e27..0000000000000 --- a/docs/csharp/misc/cs0617.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -description: "Compiler Error CS0617" -title: "Compiler Error CS0617" -ms.date: 07/20/2015 -f1_keywords: - - "CS0617" -helpviewer_keywords: - - "CS0617" -ms.assetid: a4363709-9846-4cb8-8772-f5a3ea8555ca ---- -# Compiler Error CS0617 - -'reference' is not a valid named attribute argument because it is not a valid attribute parameter type - - An attempt was made to access a [private](../language-reference/keywords/private.md) member of an attribute class. - -## Example - - The following sample generates CS0617. - -```csharp -// CS0617.cs -using System; - -[AttributeUsage(AttributeTargets.Struct | - AttributeTargets.Class | - AttributeTargets.Interface)] -public class MyClass : Attribute -{ - public int Name; - - public MyClass (int sName) - { - Name = sName; - Bad = -1; - Bad2 = -1; - } - - public readonly int Bad; - public int Bad2; -} - -[MyClass(5, Bad=0)] class Class1 {} // CS0617 -[MyClass(5, Bad2=0)] class Class2 {} -``` diff --git a/docs/csharp/misc/cs0633.md b/docs/csharp/misc/cs0633.md deleted file mode 100644 index dfd973bab902c..0000000000000 --- a/docs/csharp/misc/cs0633.md +++ /dev/null @@ -1,53 +0,0 @@ ---- -description: "Compiler Error CS0633" -title: "Compiler Error CS0633" -ms.date: 07/20/2015 -f1_keywords: - - "CS0633" -helpviewer_keywords: - - "CS0633" -ms.assetid: a24d310b-151a-45eb-b150-3407940ec24c ---- -# Compiler Error CS0633 - -The argument to the 'attribute' attribute must be a valid identifier - - Any argument that you pass to the or attributes must be a valid identifier. This means that it may not contain characters such as "+" that are illegal when they occur in identifiers. - -## Example 1 - - This example illustrates CS0633 using the . The following sample generates CS0633. - -```csharp -// CS0633a.cs -#define DEBUG -using System.Diagnostics; -public class Test -{ - [Conditional("DEB+UG")] // CS0633 - // try the following line instead - // [Conditional("DEBUG")] - public static void Main() { } -} -``` - -## Example 2 - - This example illustrates CS0633 using the . - -```csharp -// CS0633b.cs -// compile with: /target:module -#define DEBUG -using System.Runtime.CompilerServices; -public class Test -{ - [IndexerName("Invalid+Identifier")] // CS0633 - // try the following line instead - // [IndexerName("DEBUG")] - public int this[int i] - { - get { return i; } - } -} -``` diff --git a/docs/csharp/misc/cs0643.md b/docs/csharp/misc/cs0643.md deleted file mode 100644 index c360f7212d871..0000000000000 --- a/docs/csharp/misc/cs0643.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -description: "Compiler Error CS0643" -title: "Compiler Error CS0643" -ms.date: 07/20/2015 -f1_keywords: - - "CS0643" -helpviewer_keywords: - - "CS0643" -ms.assetid: beae30ff-15c2-413f-8f5c-504cdba2e57a ---- -# Compiler Error CS0643 - -'arg' duplicate named attribute argument - - A parameter, `arg`, on a user-defined attribute was specified twice. For more information, see [Attributes](/dotnet/csharp/advanced-topics/reflection-and-attributes). - -## Example - - The following sample generates CS0643: - -```csharp -// CS0643.cs -using System; -using System.Runtime.InteropServices; - -[AttributeUsage(AttributeTargets.Class)] -public class MyAttribute : Attribute -{ - public MyAttribute() - { - } - - public int x; -} - -[MyAttribute(x = 5, x = 6)] // CS0643, error setting x twice -// try the following line instead -// [MyAttribute(x = 5)] -class MyClass -{ -} - -public class MainClass -{ - public static void Main () - { - } -} -``` diff --git a/docs/csharp/misc/cs0655.md b/docs/csharp/misc/cs0655.md deleted file mode 100644 index 045c807c265ea..0000000000000 --- a/docs/csharp/misc/cs0655.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -description: "Compiler Error CS0655" -title: "Compiler Error CS0655" -ms.date: 07/20/2015 -f1_keywords: - - "CS0655" -helpviewer_keywords: - - "CS0655" -ms.assetid: 8ce340e2-eeeb-476a-8609-ab4bbaf10c44 ---- -# Compiler Error CS0655 - -'parameter' is not a valid named attribute argument because it is not a valid attribute parameter type - - See [Attributes](/dotnet/csharp/advanced-topics/reflection-and-attributes) for a discussion of valid parameter types for an attribute. - -## Example - - The following sample generates CS0655: - -```csharp -// CS0655.cs -using System; - -class MyAttribute : Attribute -{ - // decimal is not valid attribute parameter type - public decimal d = 0; - public int e = 0; -} - -[My(d = 0)] // CS0655 -// Try the following line instead: -// [My(e = 0)] -class C -{ - public static void Main() - { - } -} -``` diff --git a/docs/csharp/misc/cs0839.md b/docs/csharp/misc/cs0839.md deleted file mode 100644 index a1fb4a9b1d6e8..0000000000000 --- a/docs/csharp/misc/cs0839.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS0839" -title: "Compiler Error CS0839" -ms.date: 07/20/2015 -f1_keywords: - - "CS0839" -helpviewer_keywords: - - "CS0839" -ms.assetid: 6f2f1062-8551-4125-8880-68bfbfbcf061 ---- -# Compiler Error CS0839 - -Argument missing. - - An argument is missing in the method call. - -## To correct this error - -1. Double check the signature of the method and locate and supply the missing argument. - -## Example - - The following example generates CS0839: - -```csharp -// cs0839.cs -using System; - -namespace TestNamespace -{ - class Test - { - static int Add(int i, int j) - { - return i + j; - } - - static int Main() - { - int i = Test.Add( , 5); // CS0839 - return 1; - - } - } -} -``` diff --git a/docs/csharp/misc/cs1016.md b/docs/csharp/misc/cs1016.md deleted file mode 100644 index dc4a60f910d29..0000000000000 --- a/docs/csharp/misc/cs1016.md +++ /dev/null @@ -1,46 +0,0 @@ ---- -description: "Compiler Error CS1016" -title: "Compiler Error CS1016" -ms.date: 07/20/2015 -f1_keywords: - - "CS1016" -helpviewer_keywords: - - "CS1016" -ms.assetid: 576e6e12-981c-43b6-9e4a-29fa4e9f76e8 ---- -# Compiler Error CS1016 - -Named attribute argument expected - - Unnamed attribute arguments must appear before the named arguments. - -## Example - - The following sample generates CS1016: - -```csharp -// CS1016.cs -using System; - -[AttributeUsage(AttributeTargets.Class)] -public class HelpAttribute : Attribute -{ - public HelpAttribute(string url) // url is a positional parameter - { - m_url = url; - } - - public string Topic = null; // Topic is a named parameter - private string m_url = null; -} - -[HelpAttribute(Topic="Samples", "http://intranet/inhouse")] // CS1016 -// try the following line instead -//[HelpAttribute("http://intranet/inhouse", Topic="Samples")] -public class MainClass -{ - 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 330a157d73fca..8c8a007bad3f4 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 @@ -1,7 +1,7 @@ --- title: "Sorry, we don't have specifics on this C# error" description: "List of possible resources for compiler errors and warnings that haven't been documented yet." -ms.date: 08/04/2020 +ms.date: 12/18/2023 f1_keywords: - "CS0190" - "CS0224" @@ -25,9 +25,7 @@ f1_keywords: - "CS1734" - "CS1735" - "CS1743" - - "CS1744" - "CS1745" - - "CS1746" - "CS1747" - "CS1748" - "CS1752" @@ -141,7 +139,6 @@ f1_keywords: - "CS7033" - "CS7034" - "CS7035" - - "CS7036" - "CS7038" - "CS7041" - "CS7042" @@ -292,7 +289,6 @@ f1_keywords: - "CS8189" - "CS8190" - "CS8191" - - "CS8196" - "CS8197" - "CS8199" - "CS8202" @@ -314,7 +310,6 @@ f1_keywords: - "CS8321" - "CS8322" - "CS8323" - - "CS8324" - "CS8328" - "CS8335" - "CS8336" @@ -484,7 +479,6 @@ f1_keywords: - "CS8858" - "CS8859" - "CS8860" - - "CS8861" - "CS8863" - "CS8864" - "CS8865" @@ -512,7 +506,6 @@ f1_keywords: - "CS8901" - "CS8902" - "CS8903" - - "CS8905" - "CS8906" - "CS8907" - "CS8908" @@ -530,15 +523,8 @@ f1_keywords: - "CS8940" - "CS8941" - "CS8942" - - "CS8943" - - "CS8944" - - "CS8945" - "CS8946" - "CS8947" - - "CS8948" - - "CS8949" - - "CS8950" - - "CS8951" - "CS8953" - "CS8954" - "CS8955" @@ -548,8 +534,6 @@ f1_keywords: - "CS8961" - "CS8962" - "CS8963" - - "CS8965" - - "CS8966" - "CS8968" - "CS8970" - "CS8973" diff --git a/docs/whats-new/dotnet-docs-mod0.md b/docs/whats-new/dotnet-docs-mod0.md index 63a4f76488f5a..62768131bff0c 100644 --- a/docs/whats-new/dotnet-docs-mod0.md +++ b/docs/whats-new/dotnet-docs-mod0.md @@ -67,7 +67,7 @@ Welcome to what's new in the .NET docs for September 2023. This article lists so ### New articles -- [Compiler Error CS8964](../csharp/language-reference/compiler-messages/cs8964.md) +- [Compiler Error CS8964](../csharp/language-reference/compiler-messages/parameter-argument-mismatch.md) - [Resolve errors and warnings generated from expressions prohibited in expression trees](../csharp/language-reference/compiler-messages/expression-tree-restrictions.md) ### Updated articles