Skip to content

Commit 5a6213e

Browse files
author
Adit Sheth
committed
Resolved comments.
1 parent 033dc16 commit 5a6213e

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@ 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. Additionally, nullable reference types (`T?`) implicitly have `null` as their default value.
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.
80-
- For nullable value types or nullable reference types (`T?`), the default value is always `null`.
8180

82-
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.
8382

8483
:::code language="csharp" source="./snippets/NamedAndOptional/optional.cs" id="Snippet15":::
8584

@@ -100,15 +99,14 @@ IntelliSense uses brackets to indicate optional parameters, as shown in the foll
10099
![Screenshot showing IntelliSense quick info for the ExampleMethod method.](./media/named-and-optional-arguments/optional-examplemethod-parameters.png)
101100

102101
> [!NOTE]
103-
> You can also declare optional parameters by using the .NET <xref:System.Runtime.InteropServices.OptionalAttribute> class. `OptionalAttribute` parameters do not require a default value. However, if a default value is desired, take a look at <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> class. Additionally, nullable types (`T?`) implicitly default to `null` without needing to use these attributes.
104-
102+
> You can also declare optional parameters by using the .NET <xref:System.Runtime.InteropServices.OptionalAttribute> class. `OptionalAttribute` parameters do not require a default value. However, if a default value is desired, take a look at <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> class.
105103
### Example
106104

107105
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.
108106

109107
:::code language="csharp" source="./snippets/NamedAndOptional/optional.cs" id="Snippet2":::
110108

111-
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.
109+
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.
112110

113111
## Caller information attributes
114112

0 commit comments

Comments
 (0)