The Best of C# 10 - 13: What Every Dev Should Know!


Often, the question arises as to whether C# needs a new version every year.

While we can debate the frequency of new releases, I cannot deny that many new features have become part of my daily work as a .NET developer.

Today, I will share the new C# features from the following C# versions:

  • C# 10 (2021)
  • C# 11 (2022)
  • C# 12 (2023)
  • C# 13 (2024)

that I regularly use in my code.

Primary Constructors (C# 12)

​Primary Constructors allow me to clean up my service implementations. Instead of having the duplication of fields and constructor parameters, I use the shortened syntax that Primary Constructors provide.

It’s undoubtedly the feature I use the most, and it helps a lot with the readability of my code.

Collection Expressions (C# 12)

Another frequently used feature is Collection Expressions. When declaring arrays or lists, collection expressions provide a shorthand syntax similar to TypeScript or JavaScript.

It’s much more concise than writing the types and all the other syntactic bloat.

Required Members (C# 11)

When working with Nullable Reference Types, the required keyword is the best solution for implementing data classes.

The required keyword ensures that after the object initialization, the fields marked with it contain a value.

File-Scoped Namespaces (C# 10)

Another improvement in the reliability of my code is File-Scoped Namespaces.

Instead of nesting the whole code file inside the namespace definition, File-Scoped Namespaces allow me to save one level of indentation and improve the readability of the code.

When did you last declare types in different namespaces within the same code file? I never did that in my entire career.

Global Using Directives (C# 10)

​Global Usings are helpful for reducing the number of use statements. I mostly work with Blazor, and Global Usings allows me to define the types in a single file and make them available throughout the project.

It has some risks, but overall, I really like the opportunity we have with Global Using Directives. Often, a local using statement is still the best option.

Params Collections (C# 13)

​Params Collections is not something I use daily. But when I use the params keyword, I now mostly use collection types, such as IEnumerable or IList, with it.

Prior to C# 13, only a selected group of types was available, mostly array types.

Conclusion

It’s fair to say the latest C# versions have changed the look of most of my C# code files.

Although I usually only use a few new features with each C# version, I still see the benefit of staying current with my application and taking full advantage of the latest compiler features.

At this point, I cannot imagine going back to writing code for the .NET Framework, which limits official C# support to version 7.3.