From f55c861e25b5247035d5c15144da01c7fe2f3498 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 16 Jan 2025 07:40:45 -0800 Subject: [PATCH 1/4] Resolved Bug 44404. --- .../classes-and-structs/named-and-optional-arguments.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md index 4157d833c26cf..ca7e3e680b609 100644 --- a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md +++ b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md @@ -70,13 +70,14 @@ The following code implements the examples from this section along with some add ## Optional arguments -The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. +The definition of a method, constructor, indexer, or delegate can specify its parameters as required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Additionally, nullable reference types (`T?`) implicitly have `null` as their default value. Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. A default value must be one of the following types of expressions: - a constant expression; - an expression of the form `new ValType()`, where `ValType` is a value type, such as an [enum](../../language-reference/builtin-types/enum.md) or a [struct](../../language-reference/builtin-types/struct.md); - an expression of the form [default(ValType)](../../language-reference/operators/default.md), where `ValType` is a value type. +- For nullable value types or nullable reference types (`T?`), the default value is always `null`. Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported. For example, in the following code, instance method `ExampleMethod` is defined with one required and two optional parameters. @@ -85,7 +86,7 @@ Optional parameters are defined at the end of the parameter list, after any requ The following call to `ExampleMethod` causes a compiler error, because an argument is provided for the third parameter but not for the second. ```csharp -//anExample.ExampleMethod(3, ,4); +// anExample.ExampleMethod(3, ,4); ``` However, if you know the name of the third parameter, you can use a named argument to accomplish the task. @@ -99,7 +100,7 @@ IntelliSense uses brackets to indicate optional parameters, as shown in the foll ![Screenshot showing IntelliSense quick info for the ExampleMethod method.](./media/named-and-optional-arguments/optional-examplemethod-parameters.png) > [!NOTE] -> You can also declare optional parameters by using the .NET class. `OptionalAttribute` parameters do not require a default value. However, if a default value is desired, take a look at class. +> You can also declare optional parameters by using the .NET class. `OptionalAttribute` parameters do not require a default value. However, if a default value is desired, take a look at class. Additionally, nullable types (`T?`) implicitly default to `null` without needing to use these attributes. ### Example From 033dc160490def34395ebc3194ce8e3ba355fef1 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 16 Jan 2025 07:44:05 -0800 Subject: [PATCH 2/4] Fixed typo. --- .../classes-and-structs/named-and-optional-arguments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md index ca7e3e680b609..c31814e00d9c4 100644 --- a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md +++ b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md @@ -70,7 +70,7 @@ The following code implements the examples from this section along with some add ## Optional arguments -The definition of a method, constructor, indexer, or delegate can specify its parameters as required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Additionally, nullable reference types (`T?`) implicitly have `null` as their default value. +The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Additionally, nullable reference types (`T?`) implicitly have `null` as their default value. Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. A default value must be one of the following types of expressions: From 5a6213ecb0d753908126914a590667d56b77c257 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Fri, 17 Jan 2025 09:32:54 -0800 Subject: [PATCH 3/4] Resolved comments. --- .../named-and-optional-arguments.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md index c31814e00d9c4..49d324444be0e 100644 --- a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md +++ b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md @@ -70,16 +70,15 @@ The following code implements the examples from this section along with some add ## Optional arguments -The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Additionally, nullable reference types (`T?`) implicitly have `null` as their default value. +The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. A nullable reference type (`T?`) allows arguments to be explicitly `null` but does not inherently make a parameter optional. Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. A default value must be one of the following types of expressions: - + - a constant expression; - an expression of the form `new ValType()`, where `ValType` is a value type, such as an [enum](../../language-reference/builtin-types/enum.md) or a [struct](../../language-reference/builtin-types/struct.md); - an expression of the form [default(ValType)](../../language-reference/operators/default.md), where `ValType` is a value type. -- For nullable value types or nullable reference types (`T?`), the default value is always `null`. -Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported. For example, in the following code, instance method `ExampleMethod` is defined with one required and two optional parameters. +Optional parameters are defined at the end of the parameter list, after any required parameters. The caller must provide arguments for all required parameters and any optional parameters preceding those it specifies. Comma-separated gaps in the argument list aren't supported.For example, in the following code, instance method `ExampleMethod` is defined with one required and two optional parameters. :::code language="csharp" source="./snippets/NamedAndOptional/optional.cs" id="Snippet15"::: @@ -100,15 +99,14 @@ IntelliSense uses brackets to indicate optional parameters, as shown in the foll ![Screenshot showing IntelliSense quick info for the ExampleMethod method.](./media/named-and-optional-arguments/optional-examplemethod-parameters.png) > [!NOTE] -> You can also declare optional parameters by using the .NET class. `OptionalAttribute` parameters do not require a default value. However, if a default value is desired, take a look at class. Additionally, nullable types (`T?`) implicitly default to `null` without needing to use these attributes. - +> You can also declare optional parameters by using the .NET class. `OptionalAttribute` parameters do not require a default value. However, if a default value is desired, take a look at class. ### Example In the following example, the constructor for `ExampleClass` has one parameter, which is optional. Instance method `ExampleMethod` has one required parameter, `required`, and two optional parameters, `optionalstr` and `optionalint`. The code in `Main` shows the different ways in which the constructor and method can be invoked. :::code language="csharp" source="./snippets/NamedAndOptional/optional.cs" id="Snippet2"::: -The preceding code shows a number of examples where optional parameters aren't applied correctly. The first illustrates that an argument must be supplied for the first parameter, which is required. +The preceding code illustrates several cases where optional parameters are used correctly and incorrectly. Arguments must be supplied for all required parameters. Gaps in optional arguments must be filled with named parameters. ## Caller information attributes From bca56d5d74ce7942900afcc0f87c34777f09a56a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 21 Jan 2025 09:34:59 -0500 Subject: [PATCH 4/4] Update docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md --- .../classes-and-structs/named-and-optional-arguments.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md index 49d324444be0e..7ffb8edcf9889 100644 --- a/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md +++ b/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md @@ -100,6 +100,7 @@ IntelliSense uses brackets to indicate optional parameters, as shown in the foll > [!NOTE] > You can also declare optional parameters by using the .NET class. `OptionalAttribute` parameters do not require a default value. However, if a default value is desired, take a look at class. + ### Example In the following example, the constructor for `ExampleClass` has one parameter, which is optional. Instance method `ExampleMethod` has one required parameter, `required`, and two optional parameters, `optionalstr` and `optionalint`. The code in `Main` shows the different ways in which the constructor and method can be invoked.