DEV Community 👩‍💻👨‍💻

Cover image for 5 New C# 11 Features You Might Have Missed
ByteHide
ByteHide

Posted on • Updated on • Originally published at blog.dotnetsafer.com

5 New C# 11 Features You Might Have Missed

As you probably know, Microsoft has started to announce the arrival of the new version of its programming language: C# 11.

Among many other things, they have revealed many new features of this version will bring and as I have already commented the best features in other posts, today we are going to see the features that you might have missed because little has been said about them or because they have not had so much hype.

Let’s see what’s new in C# 11!


Generic math support

Generic Math is a feature that Microsoft introduced in .NET 6, but now with the arrival of .NET 7 they have implemented many improvements and that is why we are going to talk about it. With Generic Math you can take full advantage of operators and static APIs by combining static virtuals and the power of generics.

One of its major advantages is that it allows you to restrict the input to number like types avoiding having many similar implementations (if not almost identical). Besides, now you will be able to use different operators from generic contexts.

In addition to this new feature, generic math support has been added with different features such as:

  • Members that are static and virtual in interfaces
  • Verified user-defined operators
  • Relaxed right-shift requirements
  • Operator with an unsigned right shift

Thanks to this, it will now be possible to add static abstract members with overloadable operators (or static properties or members) to define interfaces.

And that’s not all, now some different requirements have also been created due to generic math. These requirements are divided into 3:

  • Unsigned right shift operator: Now with C# 11 you will simply have to use >>> to force any unasigned rigth-shift.
  • Relaxed shift operator requirements: Thanks to C# 11, the requirement that the second operand must be an int is eliminated and thanks to this, types that implement generic math interfaces can be used in these cases.
  • Checked and unchecked user defined operators: Now both checked and unchecked arithmetic operators can be defined. This allows the compiler, always depending on the context, to generate calls to the correct variable.

If you want to know more about this feature, I recommend you, as always, to consult the original source: Microsoft C# 11 Generic math support


Numeric IntPtr and UIntPtr

This new feature adds to the nint and nuint integral numeric types as aliases System.IntPtrandSystem.UlntPtr.

The two types nint and nuint are native-sized integers. The thing is that they are native if, for example, they are 64-bit integers and run in a 64-bit process. The same as for 32-bit scenario.

Another advantage is the performance optimization in scenarios where integer math is used and they can also be used in low-level libraries and interop scenarios.

In the following table you can find all the Integral numeric types:

Image description

If you want to know more about this feature, I recommend you, as always, to consult the original source: Microsoft Numeric IntPtr and UIntPtr


Auto-default struct

This C# 11 feature is a bit more compiler focused. For those who don’t know, a varibale of a struct type contains all the data of that struct. So you can easily distinguish if a struct is initialized, if it is uninitialized or if it has a default value. Let’s see the Microsoft example:

The structure-type array instantiation generates an array with the default values of a structure type while ignoring the parameterless costructor.

Now in C# 11, the new compiler in this version takes care to ensure that fields of any struct type are always initialized to their default value. The result of this is that if an automatic property or field is not initialized by the constructor, it will be initialized by the compiler.

If you want to know more about this feature, I recommend you, as always, to consult the original source: Microsoft auto-default struct


Pattern match Span<char> or ReadOnlySpan<char> on a constant string

As Microsoft tells us, since a couple of C# versions ago, using pattern matching you could check if a particular string has a specific constant value. Now in C# 11 opens the possibility to use the same logic for pattern matching ReadOnlySpan and Span variables.

If you want to know more about this feature, I recommend you, as always, to consult the original source: Microsoft pattern matching


Extended nameof scope

The function of nameof expressions is to generate the name of a type, variable or member as a string constant. Besides, it does not affect in any way at run time since it is evaluated and executed at compile time.

One of the utilities of the nameof expression is to maintain the argument checking code. Let’s look at the following Microsoft example:

With the new version of C# it will be possible to specify the name of any method parameter in an attribute of the method declaration using the nameof operators.

This is because when both parameter names and type parameter names are used in a nameof expression, they are always in scope.

This makes it possible to easily add attributes for nullable analysis.

If you want to know more about this feature, I recommend you, as always, to consult the original source: Microsoft extended nameof scope


All these new features in C# 11 and all the work Microsoft is doing for C# is amazing. This is not a minor release, this is a very big release and at least for me, we are in front of one of the biggest updates of the C# language.

If we remember the principles of C# until today, that we can develop from desktop applications, through web applications to mobile. And let’s not forget that C# is also a popular game developing programming language (Unity).

Of course, there are still a couple of months to go before the official release of C# 11 and knowing Microsoft, we are sure that it has something else to surprise us until then. What do you think in general about C# 11? Was it what you expected? Do you miss any feature or better?

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Good news! You can update to dark mode in your DEV settings.