Skip to content

Commit ef50ddf

Browse files
shethaaditAdit Sheth
and
Adit Sheth
authored
Clarify Optional Parameters and Nullable Optional Parameter Behavior in C#. (#44408)
* Resolved Bug 44404. * Fixed typo. * Resolved comments. --------- Co-authored-by: Adit Sheth <[email protected]>
1 parent 4da0d8d commit ef50ddf

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,22 @@ The following code implements the examples from this section along with some add
7070

7171
## Optional arguments
7272

73-
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.
73+
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.
7474

7575
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:
76-
76+
7777
- a constant expression;
7878
- 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);
7979
- an expression of the form [default(ValType)](../../language-reference/operators/default.md), where `ValType` is a value type.
8080

81-
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.
81+
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.
8282

8383
:::code language="csharp" source="./snippets/NamedAndOptional/optional.cs" id="Snippet15":::
8484

8585
The following call to `ExampleMethod` causes a compiler error, because an argument is provided for the third parameter but not for the second.
8686

8787
```csharp
88-
//anExample.ExampleMethod(3, ,4);
88+
// anExample.ExampleMethod(3, ,4);
8989
```
9090

9191
However, if you know the name of the third parameter, you can use a named argument to accomplish the task.
@@ -107,7 +107,7 @@ In the following example, the constructor for `ExampleClass` has one parameter,
107107

108108
:::code language="csharp" source="./snippets/NamedAndOptional/optional.cs" id="Snippet2":::
109109

110-
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.
110+
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.
111111

112112
## Caller information attributes
113113

0 commit comments

Comments
 (0)