I was lucky enough to hear Mads Torgersen (The Lead Designer of the C# language) speak at NDC London about some of the plans the team has for the next version of C#. Many of these new features are in the preview stage, and can be enabled by using the c# preview flag in your csproj file. The C# team welcomes feedback on the different use cases for new feature, especially during these early previews. As this is a preview of new language features, don’t blame me if this all changes before it all ships in Novembers release!

1<PropertyGroup>
2   <LangVersion>preview</LangVersion>
3</PropertyGroup>

Dictionary Expressions

This is an extension of the existing collection expressions that we have had since C# 12, but this time for dictionaries.

The syntax is going to look a bit like

1IList<int> foo = [1, 2, 3];
2IDictionary<int, string> bar = [1: "one", 2: "two", 3: "three"];

Dictionary Expressions

Simple lambda parameters with modifiers

1WithOut w1 = (string s) => s.Length;
2WithOut w2 = (s) => s.Length;
3WithOut w3 = (string s, out int i) => i = s.Length;
4WithOut w4 = (s, out i) => i = s.Length;

Simple lambda parameters with modifiers

Unbound generic types in nameof

1var t = typeof(List<>);
2var n = nameof(List<>);

Unbound generic types in nameof

Null conditional assignment

1var c = C.GetOne();
2c.P = "Hello";
3c.E += () => { };
4c?.P = "Hello";
5c?.E += () => { };

Null conditional assignment

Partial events and constructors

1partial class Partials
2{
3  public partial void M(int i);
4  public partial string P { get; }
5  public partial string this[int i] { get; }
6  public partial event Handler? E;
7  public partial Partials();
8}

Partial events and constructors

Field access in auto properties

1class Fields
2{
3  public string? FirstName { get; set; }
4  public string? LastName { get => field; set => field = value?.Trim(); }
5}

note the field keyword is a potential breaking change if you have field variables in your code.

Field access in auto properties

This last one revealed a couple of gems of information that I found interesting. Creating a class called var can essentially disable the var keyword, and if you declare a variable called _ it can disable the discard operator. Neither of these things I would want to do in production code, but interesting that the language allows them.

Extensions

We have had extension methods for a while, but what about extension members?

Extension members

 1public static class Enumerable
 2{
 3    // New extension declaration
 4    extension(IEnumerable source) { ... }
 5    
 6    // Classic extension method
 7    public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source) { ... }
 8    
 9    // Non-extension member
10    public static IEnumerable<int> Range(int start, int count) { ... } 
11}

This was a fascinating look at what is coming up for C#, and it will be interesting to see how these new ways of writing code will get adopted.

If you have enjoyed this article and want to get a monthly email with all my latest articles, please sign up for my newsletter. If you have any questions or comments, please feel free to reach out or leave a comment below.