Skip to content

Commit 6fd5dbc

Browse files
authored
Update default param val attr (dotnet#9356)
1 parent 99aef61 commit 6fd5dbc

File tree

4 files changed

+41
-37
lines changed

4 files changed

+41
-37
lines changed

snippets/csharp/System.Runtime.InteropServices/DefaultParameterValueAttribute/Overview/sample.cs

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//<snippet1>
2+
using System;
3+
using System.Runtime.InteropServices;
4+
5+
public class Program
6+
{
7+
public static void MethodWithDefaultParam([Optional, DefaultParameterValue("DEFAULT_PARAM_VALUE")] string str)
8+
{
9+
Console.WriteLine($"The passed value is: {str}");
10+
}
11+
12+
public static void Main()
13+
{
14+
MethodWithDefaultParam(); // The passed value is: DEFAULT_PARAM_VALUE
15+
MethodWithDefaultParam("NEW_VALUE"); // The passed value is: NEW_VALUE
16+
}
17+
}
18+
//</snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//<snippet2>
2+
using System.Runtime.InteropServices;
3+
4+
public class Program
5+
{
6+
public static void MethodWithObjectDefaultAttr1([Optional, DefaultParameterValue(123)] object obj) {} // OK
7+
public static void MethodWithObjectDefaultAttr2([Optional, DefaultParameterValue("abc")] object obj) {} // OK
8+
public static void MethodWithObjectDefaultAttr3([Optional, DefaultParameterValue(null)] object? obj) {} // OK
9+
10+
public static void MethodWithObjectDefaultParam1(object obj = 123) {} // CS1763
11+
public static void MethodWithObjectDefaultParam2(object obj = "abc") {} // CS1763
12+
public static void MethodWithObjectDefaultParam3(object obj? = null) {} // OK
13+
}
14+
//</snippet2>

xml/System.Runtime.InteropServices/DefaultParameterValueAttribute.xml

+9-10
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,17 @@
6464
## Remarks
6565
The <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> attribute allows you to specify a default parameter value in a language that does not otherwise support default parameters. After you apply this attribute to your code, languages that support default parameters can use the specified value as a default parameter.
6666
67-
Note that the <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> does not add support for default parameters to languages that do not support this feature. For example, if you use the <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> with a method written in C#, which does not support default parameters, you cannot use the default parameter when calling the method from C#. You can use the default parameter only when calling the method in a language such as C++ that does support the feature.
68-
6967
The <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> is particularly useful to specify default parameters for methods of a COM interop interface.
7068
71-
72-
7369
## Examples
74-
The following code example demonstrates how to apply the <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> attribute to a parameter of a method written in C#.
70+
71+
The following code example demonstrates how to apply the <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> attribute to a parameter of a method written in C#. The snippet also uses the <xref:System.Runtime.InteropServices.OptionalAttribute> attribute to enable the method to be called without any arguments.
72+
73+
:::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/DefaultParameterValueAttribute/Overview/sample1.cs" id="Snippet1":::
7574
76-
:::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/DefaultParameterValueAttribute/Overview/sample.cs" id="Snippet1":::
75+
There are some behavior differences between [C# optional arguments](/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments) and using <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> with <xref:System.Runtime.InteropServices.OptionalAttribute>. The following code snippet demonstrates these differences.
76+
77+
:::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/DefaultParameterValueAttribute/Overview/sample2.cs" id="Snippet2":::
7778
7879
]]></format>
7980
</remarks>
@@ -132,12 +133,10 @@
132133
## Remarks
133134
Use this constructor to apply the <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> attribute to a parameter written in a language such as Microsoft Visual C# that does not support default parameters.
134135
135-
136-
137136
## Examples
138-
The following code example demonstrates how to apply the <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> attribute to a parameter of a method written in C#.
137+
The following code example demonstrates how to apply the <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute> attribute to a parameter of a method written in C#. The <xref:System.Runtime.InteropServices.OptionalAttribute> attribute is also used to enable the method to be called without any arguments.
139138
140-
:::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/DefaultParameterValueAttribute/Overview/sample.cs" id="Snippet1":::
139+
:::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/DefaultParameterValueAttribute/Overview/sample1.cs" id="Snippet1":::
141140
142141
]]></format>
143142
</remarks>

0 commit comments

Comments
 (0)