Skip to content

Commit

Permalink
Add example for optional attr (#9355)
Browse files Browse the repository at this point in the history
* Add example for optional attr

* Fix up space issues

Co-authored-by: Genevieve Warren <[email protected]>
  • Loading branch information
Rageking8 and gewarren authored Oct 23, 2023
1 parent 6cc8bf1 commit 05d4f99
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//<snippet1>
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
}
}
//</snippet1>
7 changes: 7 additions & 0 deletions xml/System.Runtime.InteropServices/OptionalAttribute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
You can apply this attribute to parameters.
Optional parameters are not supported by all languages.
<xref:System.Runtime.InteropServices.OptionalAttribute> is sometimes used in conjunction with <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute>.
## Examples
The following code example demonstrates how to apply the <xref:System.Runtime.InteropServices.OptionalAttribute> 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":::
]]></format>
</remarks>
Expand Down

0 comments on commit 05d4f99

Please sign in to comment.