Skip to content

Commit

Permalink
Some edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
BillWagner committed Feb 3, 2025
1 parent 42bd8fd commit 09a4851
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 3 additions & 5 deletions docs/csharp/language-reference/operators/nameof.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ A `nameof` expression produces the name of a variable, type, or member as the st

:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/NameOfOperator.cs" id="Examples":::

You can use a `nameof` expression to make the argument-checking code more maintainable:
The preceding example using `List<>` is supported in C# 14 and later. You can use a `nameof` expression to make the argument-checking code more maintainable:

:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/NameOfOperator.cs" id="ExceptionMessage":::
:::code language="csharp" source="snippets/shared/NameOfOperator.cs" id="ExceptionMessage":::

Beginning with C# 11, you can use a `nameof` expression with a method parameter inside an [attribute](../../advanced-topics/reflection-and-attributes/index.md) on a method or its parameter. The following code shows how to do that for an attribute on a method, a local function, and the parameter of a lambda expression:

:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/NameOfOperator.cs" id="SnippetNameOfParameter":::
:::code language="csharp" source="snippets/shared/NameOfOperator.cs" id="SnippetNameOfParameter":::

A `nameof` expression with a parameter is useful when you use the [nullable analysis attributes](../attributes/nullable-analysis.md) or the [CallerArgumentExpression attribute](../attributes/caller-information.md#argument-expressions).

When the operand is a [verbatim identifier](../tokens/verbatim.md), the `@` character isn't part of the name, as the following example shows:

:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/NameOfOperator.cs" id="Verbatim":::

Beginning with C# 14, the argument can be an unbound generic type, such as `List<>`. The `nameof(List<>)` expression evaluates to `List`. If you use a closed generic type, such as `List<int>`, the output is the generic type, not the closed instantiation: `List`.

## C# language specification

For more information, see the [Nameof expressions](~/_csharpstandard/standard/expressions.md#12823-the-nameof-operator) section of the [C# language specification](~/_csharpstandard/standard/README.md), and the [C# 11 - Extended `nameof` scope](~/_csharplang/proposals/csharp-11.0/extended-nameof-scope.md) feature specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static void Examples()
// <SnippetExamples>
Console.WriteLine(nameof(System.Collections.Generic)); // output: Generic
Console.WriteLine(nameof(List<int>)); // output: List
Console.WriteLine(nameof(List<>)); // output: List
Console.WriteLine(nameof(List<int>.Count)); // output: Count
Console.WriteLine(nameof(List<int>.Add)); // output: Add

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ImplicitUsings>true</ImplicitUsings>
<NoWarn>7022</NoWarn>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 09a4851

Please sign in to comment.