What is new with C#

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!

<PropertyGroup>
   <LangVersion>preview</LangVersion>
</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

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

Dictionary Expressions

Simple lambda parameters with modifiers

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

Simple lambda parameters with modifiers

Unbound generic types in nameof

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

Unbound generic types in nameof

Null conditional assignment

var c = C.GetOne();
c.P = "Hello";
c.E += () => { };
c?.P = "Hello";
c?.E += () => { };

Null conditional assignment

Partial events and constructors

partial class Partials
{
  public partial void M(int i);
  public partial string P { get; }
  public partial string this[int i] { get; }
  public partial event Handler? E;
  public partial Partials();
}

Partial events and constructors

Field access in auto properties

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

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

public static class Enumerable
{
    // New extension declaration
    extension(IEnumerable source) { ... }
    
    // Classic extension method
    public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source) { ... }
    
    // Non-extension member
    public static IEnumerable<int> Range(int start, int count) { ... } 
}

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.

Comments

comments powered by Disqus