diff --git a/release-notes/11.0/preview/preview1/csharp.md b/release-notes/11.0/preview/preview1/csharp.md index 32d54dcfc4..a5e5600726 100644 --- a/release-notes/11.0/preview/preview1/csharp.md +++ b/release-notes/11.0/preview/preview1/csharp.md @@ -1,8 +1,31 @@ # C# in .NET 11 Preview 1 - Release Notes -.NET 11 Preview 1 includes the following C# features & enhancements. +.NET 11 Preview 1 includes two new features in C#: -This Preview 1 release does not introduce new C# feature additions. +- [Collection expression arguments](#collection-expression-arguments) +- [Extended layout support](#extended-layout-support) + +## Collection expression arguments + +This feature supports scenarios where a collection expression doesn't produce the collection type you need. You might want to pre-allocate storage because you know how you'll use the collection. For sorted collections, you may want to specify a different expression to compare items in the collection. For dictionaries, you may need a different expression to compare or find keys. + +You specify arguments to the constructor of the collection in a `with()` element in the [collection expression](https://learn.microsoft.com/dotnet/csharp/language-reference/operators/collection-expressions). The `with()` element must be the first element in the collection exprssion. You can specify values for any of the arguments to a specified constructor for the collection, as shown in the following example: + +```csharp +// Initialize to twice the capacity since we'll have to add +// more values later. +List names = [with(capacity: values.Count * 2), .. values]; +``` + +The compiler generates code to call the [`List` constructor](https://learn.microsoft.com/dotnet/api/system.collections.generic.list-1.-ctor#system-collections-generic-list-1-ctor(system-int32)) with the `capacity` argument set to `values.Count * 2`. + +You can learn more about this new feature in the [What's new in C# 15](https://learn.microsoft.com/dotnet/csharp/whats-new/csharp-15) article. You'll find a longer description, and links to other documentation on the feature. This information includes information on how to specify arguments when the target type is an interface, such as `IEnumerable`. + +This feature will integrate with [dictionary expressions](https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md#working-set-c), which is in progress now. + +## Extended layout support + +The C# compiler emits the `TypeAttributes.ExtendedLayout` for types that have the `System.Runtime.InteropServices.ExtendedLayoutAttribute` applied. This is primarily intended for the .NET runtime team to use for types in interop scenarios. C# updates: