Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nameof for open generic types, first class span implicit conversions, and implicit lambda parameters with modifiers #44641

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

BillWagner
Copy link
Member

@BillWagner BillWagner commented Jan 31, 2025

This is the first C# 14 feature I've added, so create the C# 14 file, and update the language version tables accordingly.

Many of the files that have been updated are updating links and anchors. Major changes are:

📄 File 🔗 Preview link
docs/csharp/language-reference/builtin-types/built-in-types.md "Built-in types"
docs/csharp/language-reference/operators/lambda-expressions.md docs/csharp/language-reference/operators/lambda-expressions
docs/csharp/language-reference/operators/lambda-operator.md Lambda expression (=>) operator defines a lambda expression
docs/csharp/language-reference/operators/nameof.md "The nameof expression - evaluate the text name of a symbol"
docs/csharp/language-reference/operators/type-testing-and-cast.md Type-testing operators and cast expressions - is, as, typeof, and casts
docs/csharp/language-reference/operators/user-defined-conversion-operators.md "User-defined explicit and implicit conversion operators - provide conversions to different types"
docs/csharp/programming-guide/types/casting-and-type-conversions.md Casting and type conversions (C# Programming Guide)
docs/csharp/whats-new/csharp-14.md What's new in C# 14

Internal previews

Toggle expand/collapse
📄 File 🔗 Preview link
.github/workflows/snippets5000.yml Controls when the action will run. Triggers the workflow on push or pull request
docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md How to safely cast by using pattern matching and the is and as operators
docs/csharp/index.yml highlightedContent section (Optional; Remove if not applicable.)
docs/csharp/language-reference/builtin-types/built-in-types.md "Built-in types"
docs/csharp/language-reference/builtin-types/collections.md Collections
docs/csharp/language-reference/builtin-types/nullable-value-types.md docs/csharp/language-reference/builtin-types/nullable-value-types
docs/csharp/language-reference/compiler-messages/assembly-references.md Resolve errors and warnings related to assembly references
docs/csharp/language-reference/compiler-messages/cs0039.md "Compiler Error CS0039"
docs/csharp/language-reference/compiler-messages/cs0413.md Compiler Error CS0413
docs/csharp/language-reference/compiler-messages/params-arrays.md Errors and warnings related to the params modifier on method parameters
docs/csharp/language-reference/configure-language-version.md docs/csharp/language-reference/configure-language-version
docs/csharp/language-reference/keywords/index.md C# Keywords
docs/csharp/language-reference/operators/index.md "Operators and expressions - List all operators and expression"
docs/csharp/language-reference/operators/is.md is operator (C# reference)
docs/csharp/language-reference/operators/lambda-expressions.md docs/csharp/language-reference/operators/lambda-expressions
docs/csharp/language-reference/operators/lambda-operator.md Lambda expression (=>) operator defines a lambda expression
docs/csharp/language-reference/operators/nameof.md "The nameof expression - evaluate the text name of a symbol"
docs/csharp/language-reference/operators/operator-overloading.md Operator overloading - predefined unary, arithmetic, equality and comparison operators
docs/csharp/language-reference/operators/patterns.md docs/csharp/language-reference/operators/patterns
docs/csharp/language-reference/operators/type-testing-and-cast.md Type-testing operators and cast expressions - is, as, typeof, and casts
docs/csharp/language-reference/operators/user-defined-conversion-operators.md "User-defined explicit and implicit conversion operators - provide conversions to different types"
docs/csharp/language-reference/statements/lock.md The lock statement - ensure exclusive access to a shared resource
docs/csharp/misc/cs0077.md docs/csharp/misc/cs0077
docs/csharp/misc/cs0244.md Compiler Error CS0244
docs/csharp/misc/cs0837.md Compiler Error CS0837
docs/csharp/programming-guide/types/casting-and-type-conversions.md Casting and type conversions (C# Programming Guide)
docs/csharp/specification/toc.yml docs/csharp/specification/toc
docs/csharp/toc.yml Taken from https://github.com/dotnet/roslyn/wiki/Samples-and-Walkthroughs
docs/csharp/whats-new/csharp-11.md What's new in C# 11
docs/fundamentals/code-analysis/style-rules/ide0082.md Convert typeof to nameof (IDE0082)

Note

This table shows preview links for the 30 files with the most changes. For preview links for other files in this PR, select OpenPublishing.Build Details within checks.

@BillWagner
Copy link
Member Author

Let's hold this until the first .NET 10 preview comes out.

That will fix the snippets 5000 build, and the new example will compile.

@BillWagner BillWagner requested a review from CamSoper February 4, 2025 22:27
@BillWagner BillWagner changed the title Add support for nameof and open generic types Add support for nameof and open generic types, and first class span implicit conversions Feb 4, 2025
@BillWagner BillWagner marked this pull request as ready for review February 5, 2025 12:56
@BillWagner BillWagner requested review from IEvangelist, a team and gewarren as code owners February 5, 2025 12:56
@BillWagner
Copy link
Member Author

@CamSoper This is ready for review. The snippets build will pass once .NET 10 preview 1 is public. I'd like to merge it once that's available.


## The `field` keyword

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
The [`field`](../language-reference/keywords/field.md) contextual keyword is in C# 14 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see us go a little deeper into field since it will be one of the marquee features.

Suggested change
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.
The token `field` enables you to write a property accessor body without declaring an explicit backing field. `field` is replaced with a compiler synthesized backing field.
If you previously wrote:
```c#
Example

You can now simplify your code to:

Example

You can declare a body for one or both accessors for a field backed property.

The field contextual keyword also appeared in C# 13 as a preview feature.

docs/csharp/whats-new/csharp-14.md Outdated Show resolved Hide resolved
@BillWagner BillWagner force-pushed the unbound-nameof branch 3 times, most recently from b5ff933 to 4f0d3d1 Compare February 10, 2025 21:46

- [`nameof` supports unbound generic types](#unbound-generic-types-and-nameof)

C# 14 includes the [`field`](#the-field-keyword) contextual keyword as a preview feature.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a normal preview feature I think. Not sure why it isn't just part of the list. We expect to release it in C# 14.


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):

- [`nameof` supports unbound generic types](#unbound-generic-types-and-nameof)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing implicit span conversion in list.


## The `field` keyword

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see us go a little deeper into field since it will be one of the marquee features.

Suggested change
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.
The token `field` enables you to write a property accessor body without declaring an explicit backing field. `field` is replaced with a compiler synthesized backing field.
If you previously wrote:
```c#
Example

You can now simplify your code to:

Example

You can declare a body for one or both accessors for a field backed property.

The field contextual keyword also appeared in C# 13 as a preview feature.

docs/csharp/whats-new/csharp-14.md Outdated Show resolved Hide resolved
docs/csharp/whats-new/csharp-14.md Outdated Show resolved Hide resolved

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.

## Implicit span conversions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to develop a plan for what happens when we rename a feature between inception and release. I like your new name much better, but wonder if we lose continuity if we don't add it to the Feature Status page. And AKA in the first column?

Obviously, we need to include Julien in that. This is a good example for us to work with.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked this as it seemed the bulk of the new language feature was on the additional conversions and conversion priority.


## Implicit span conversions

C# 14 introduces first-class support for <xref:System.Span`1?displayProperty=fullName> and <xref:System.ReadOnlySpan`1?displayProperty=fullName> in the language. This support involves new implicit conversions allowing more natural programming with these integral types.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you use the feature status name immediately, but it is subtle.

docs/csharp/whats-new/csharp-14.md Outdated Show resolved Hide resolved

## Unbound generic types and nameof

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>`, could be used to produce `List`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is produce the best name here? Retrieve?

very small comment

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.
BillWagner and others added 6 commits February 13, 2025 14:59
Get this ready for the first preview
Add updates to C# 14 for the First class Span types.
Co-authored-by: Genevieve Warren <[email protected]>
Fixes dotnet#44887 Publish the speclet and update the language reference to allow modifiers on implicitly typed lambda expressions.
Co-authored-by: Kathleen Dollard <[email protected]>
@BillWagner BillWagner changed the title Add support for nameof and open generic types, and first class span implicit conversions Add support for nameof for open generic types, first class span implicit conversions, and implicit lambda parameters with modifiers Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment