From a2393a3d325f01345fe8479c916e235c4b7d25f8 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Mon, 2 Oct 2023 21:38:01 +0800 Subject: [PATCH 1/2] Add example for optional attr --- .../OptionalAttribute/Overview/Project.csproj | 8 ++++++++ .../OptionalAttribute/Overview/sample.cs | 18 ++++++++++++++++++ .../OptionalAttribute.xml | 9 +++++++++ 3 files changed, 35 insertions(+) create mode 100644 snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/Project.csproj create mode 100644 snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/sample.cs diff --git a/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/Project.csproj b/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/Project.csproj new file mode 100644 index 00000000000..c02dc5044e7 --- /dev/null +++ b/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/Project.csproj @@ -0,0 +1,8 @@ + + + + Library + net6.0 + + + \ No newline at end of file diff --git a/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/sample.cs b/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/sample.cs new file mode 100644 index 00000000000..9987282ed59 --- /dev/null +++ b/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/sample.cs @@ -0,0 +1,18 @@ +// +using System; +using System.Runtime.InteropServices; + +public class Program +{ + public static void MethodWithOptionalAttribute([Optional] string str) + { + Console.WriteLine($"str is null: {str == null}"); + } + + public static void Main() + { + MethodWithOptionalAttribute(); // str is null: True + MethodWithOptionalAttribute("abc"); // str is null: False + } +} +// \ No newline at end of file diff --git a/xml/System.Runtime.InteropServices/OptionalAttribute.xml b/xml/System.Runtime.InteropServices/OptionalAttribute.xml index 24a03f64383..68f66b09393 100644 --- a/xml/System.Runtime.InteropServices/OptionalAttribute.xml +++ b/xml/System.Runtime.InteropServices/OptionalAttribute.xml @@ -62,6 +62,15 @@ You can apply this attribute to parameters. Optional parameters are not supported by all languages. + + is sometimes used in conjunction with . + + + +## Examples + The following code example demonstrates how to apply the attribute to a parameter of a method written in C#. + + :::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/sample.cs" id="Snippet1"::: ]]> From 10552d931e4fb50a914d0aa533df2ac48e2159b4 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:45:10 +0800 Subject: [PATCH 2/2] Fix up space issues Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../OptionalAttribute.xml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/xml/System.Runtime.InteropServices/OptionalAttribute.xml b/xml/System.Runtime.InteropServices/OptionalAttribute.xml index 68f66b09393..7a282a45144 100644 --- a/xml/System.Runtime.InteropServices/OptionalAttribute.xml +++ b/xml/System.Runtime.InteropServices/OptionalAttribute.xml @@ -63,14 +63,12 @@ Optional parameters are not supported by all languages. - is sometimes used in conjunction with . - - - + is sometimes used in conjunction with . ## Examples - The following code example demonstrates how to apply the attribute to a parameter of a method written in C#. + +The following code example demonstrates how to apply the attribute to a parameter of a method written in C#. - :::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/sample.cs" id="Snippet1"::: +:::code language="csharp" source="~/snippets/csharp/System.Runtime.InteropServices/OptionalAttribute/Overview/sample.cs" id="Snippet1"::: ]]>