Skip to content

Commit b143973

Browse files
committed
Add support for nameof and open generic types
Fixes dotnet#44182 Add that `nameof` now supports open generic types. This is the first C# 14 feature I've added, so create the C# 14 file, and update the language version tables accordingly.
1 parent d04c1eb commit b143973

File tree

8 files changed

+58
-13
lines changed

8 files changed

+58
-13
lines changed

.openpublishing.redirection.csharp.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4282,7 +4282,7 @@
42824282
},
42834283
{
42844284
"source_path_from_root": "/docs/csharp/whats-new.md",
4285-
"redirect_url": "/dotnet/csharp/whats-new/csharp-13",
4285+
"redirect_url": "/dotnet/csharp/whats-new/csharp-14",
42864286
"redirect_document_id": true
42874287
},
42884288
{

docs/csharp/index.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ highlightedContent:
3232
# Card
3333
- title: What's new
3434
itemType: whats-new
35-
url: ./whats-new/csharp-13.md
35+
url: ./whats-new/csharp-14.md
3636
# Card
3737
- title: Learn C# video series
3838
itemType: video

docs/csharp/language-reference/configure-language-version.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Configure language version
33
description: Learn how to override the default C# language version manually. The C# compiler can support any language version up to the version in the installed SDK.
44
ms.custom: "updateeachrelease"
5-
ms.date: 09/17/2024
5+
ms.date: 01/31/2025
66
---
77

88
# Configure C# language version
@@ -17,7 +17,7 @@ If you need a specific language version that differs from the one automatically
1717

1818
> [!WARNING]
1919
>
20-
> Setting the `LangVersion` element to `latest` is discouraged. The `latest` setting means the installed compiler uses its latest version. That can change from machine to machine, making builds unreliable. In addition, it enables language features that may require runtime or library features not included in the current SDK.
20+
> Setting the `LangVersion` element to `latest` is discouraged. The `latest` setting means the installed compiler uses its latest version. The value of `latest` can change from machine to machine, making builds unreliable. In addition, it enables language features that might require runtime or library features not included in the current SDK.
2121
2222
If you must specify your C# version explicitly, you can do so in several ways:
2323

@@ -28,19 +28,19 @@ If you must specify your C# version explicitly, you can do so in several ways:
2828
> [!TIP]
2929
> You can see the language version in Visual Studio in the project properties page. Under the *Build* tab, the *Advanced* pane displays the version selected.
3030
>
31-
> To know what language version you're currently using, put `#error version` (case sensitive) in your code. This makes the compiler report a compiler error, CS8304, with a message containing the compiler version being used and the current selected language version. For more information about this pragma, see [#error (C# Reference)](preprocessor-directives.md#error-and-warning-information).
31+
> To know what language version you're currently using, put `#error version` (case sensitive) in your code. This pragma makes the compiler report a compiler error, CS8304, with a message containing the compiler version used and the current selected language version. For more information about this pragma, see [#error (C# Reference)](preprocessor-directives.md#error-and-warning-information).
3232
3333
## Edit the project file
3434

35-
You can set the language version in your project file. For example, if you explicitly want access to preview features, add an element like this:
35+
You can set the language version in your project file. For example, if you explicitly want access to preview features, add an element like the following example:
3636

3737
```xml
3838
<PropertyGroup>
3939
<LangVersion>preview</LangVersion>
4040
</PropertyGroup>
4141
```
4242

43-
The value `preview` uses the latest available preview C# language version that your compiler supports.
43+
The value `preview` uses the latest available preview C# language version your compiler supports.
4444

4545
## Configure multiple projects
4646

@@ -58,7 +58,7 @@ Builds in all subdirectories of the directory containing that file now use the p
5858

5959
> [!NOTE]
6060
>
61-
> The versions for C# and VB are different. Don't use the *Directory.Build.Props* file for a folder where subdirectories contain projects for both languages. The versions won't match.
61+
> The versions for C# and VB are different. Don't use the *Directory.Build.Props* file for a folder where subdirectories contain projects for both languages. The versions don't match.
6262
6363
## C# language version reference
6464

docs/csharp/language-reference/includes/default-langversion-table.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ms.custom: "updateeachrelease"
44

55
| Target | Version | C# language version default |
66
|------------------|---------|-----------------------------|
7+
| .NET | 10.x | C# 14 |
78
| .NET | 9.x | C# 13 |
89
| .NET | 8.x | C# 12 |
910
| .NET | 7.x | C# 11 |

docs/csharp/language-reference/includes/langversion-table.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ms.custom: "updateeachrelease"
77
| `preview` | The compiler accepts all valid language syntax from the latest preview version. |
88
| `latest` | The compiler accepts syntax from the latest released version of the compiler (including minor version). |
99
| `latestMajor`<br>or `default` | The compiler accepts syntax from the latest released major version of the compiler. |
10+
| `14.0` | The compiler accepts only syntax that is included in C# 14 or lower. |
1011
| `13.0` | The compiler accepts only syntax that is included in C# 13 or lower. |
1112
| `12.0` | The compiler accepts only syntax that is included in C# 12 or lower. |
1213
| `11.0` | The compiler accepts only syntax that is included in C# 11 or lower. |

docs/csharp/language-reference/operators/nameof.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "The nameof expression - evaluate the text name of a symbol"
33
description: "The C# `nameof` expression produces the name of its operand. You use it whenever you need to use the name of a symbol as text"
4-
ms.date: 11/28/2022
4+
ms.date: 01/31/2025
55
f1_keywords:
66
- "nameof_CSharpKeyword"
77
- "nameof"
@@ -14,21 +14,23 @@ helpviewer_keywords:
1414

1515
A `nameof` expression produces the name of a variable, type, or member as the string constant. A `nameof` expression is evaluated at compile time and has no effect at run time. When the operand is a type or a namespace, the produced name isn't [fully qualified](~/_csharpstandard/standard/basic-concepts.md#783-fully-qualified-names). The following example shows the use of a `nameof` expression:
1616

17-
[!code-csharp-interactive[nameof expression](snippets/shared/NameOfOperator.cs#Examples)]
17+
:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/NameOfOperator.cs" id="Examples":::
1818

1919
You can use a `nameof` expression to make the argument-checking code more maintainable:
2020

21-
[!code-csharp[nameof and argument check](snippets/shared/NameOfOperator.cs#ExceptionMessage)]
21+
:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/NameOfOperator.cs" id="ExceptionMessage":::
2222

2323
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:
2424

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

2727
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).
2828

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

31-
[!code-csharp-interactive[nameof verbatim](snippets/shared/NameOfOperator.cs#Verbatim)]
31+
:::code language="csharp" interactive="try-dotnet-method" source="snippets/shared/NameOfOperator.cs" id="Verbatim":::
32+
33+
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`.
3234

3335
## C# language specification
3436

docs/csharp/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ items:
148148
items:
149149
- name: C# 14
150150
items:
151+
- name: What's new in C# 14
152+
href: whats-new/csharp-14.md
151153
- name: Breaking changes since C# 13
152154
href: ../../_roslyn/docs/compilers/CSharp/Compiler%20Breaking%20Changes%20-%20DotNet%2010.md
153155
- name: C# 13

docs/csharp/whats-new/csharp-14.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: What's new in C# 14
3+
description: Get an overview of the new features in C# 13.
4+
ms.date: 01/31/2025
5+
ms.topic: whats-new
6+
---
7+
# What's new in C# 14
8+
9+
C# 14 includes the following new features. You can try these features using the latest [Visual Studio 2022](https://visualstudio.microsoft.com/vs/preview/) version or the [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet):
10+
11+
- [`nameof` supports unbound generic types](#unbound-generic-types-and-nameof)
12+
13+
C# 14 includes the [`field`](#the-field-keyword) contextual keyword as a preview feature.
14+
15+
C# 14 is supported on **.NET 10**. For more information, see [C# language versioning](../language-reference/configure-language-version.md).
16+
17+
You can download the latest .NET 10 SDK from the [.NET downloads page](https://dotnet.microsoft.com/download). You can also download [Visual Studio 2022](https://visualstudio.microsoft.com/vs/), which includes the .NET 10 SDK.
18+
19+
New features are added to the "What's new in C#" page when they're available in public preview releases. The [working set](https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md#working-set) section of the [roslyn feature status page](https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md) tracks when upcoming features are merged into the main branch.
20+
21+
You can find any breaking changes introduced in C# 14 in our article on [breaking changes](~/_roslyn/docs/compilers/CSharp/Compiler%20Breaking%20Changes%20-%20DotNet%2010.md).
22+
23+
[!INCLUDE [released-version-feedback](./includes/released-feedback.md)]
24+
25+
## The `field` keyword
26+
27+
The [`field`](../language-reference/keywords/field.md) contextual keyword is in C# 13 as a preview feature. The token `field` accesses the compiler synthesized backing field in a property accessor. It enables you to write an accessor body without declaring an explicit backing field in your type declaration. You can declare a body for one or both accessors for a field backed property.
28+
29+
The `field` feature is released as a preview feature. We want to learn from your experiences using it. There's a potential breaking change or confusion reading code in types that also include a field named `field`. You can use `@field` or `this.field` to disambiguate between the `field` keyword and the identifier.
30+
31+
[!INCLUDE[field-preview](../includes/field-preview.md)]
32+
33+
If you try this feature and have feedback, add it to the [feature issue](https://github.com/dotnet/csharplang/issues/140) in the `csharplang` repository.
34+
35+
## Unbound generic types and nameof
36+
37+
Beginning with C# 14, the argument to `nameof` can be an unbound generic type. For example, `nameof(List<>)` evaluates to `List`. In earlier versions of C#, only closed generic types, such as `List<int>` had to be used to produce `List`.
38+
39+
<!-- Add link to What's new in .NET 10 once it's published -->

0 commit comments

Comments
 (0)