5 New C# 11 Features You Might Have Missed
Tags: csharp, dotnet, news, beginners
Published: 2022-07-26 14:17:00

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](https://devblogs.microsoft.com/dotnet/preview-features-in-net-6-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**](https://devblogs.microsoft.com/dotnet/dotnet-7-generic-math/) 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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#generic-math-support) 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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#generic-math-support):

-   [**Unsigned right shift operator**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift)**:** Now with C# 11 you will simply have to use `>>>` to force any unasigned rigth-shift.
-   [**Relaxed shift operator requirements**](https://github.com/dotnet/csharplang/issues/4666)**:** 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**](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/checked-and-unchecked)**:** 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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#generic-math-support)

---

## Numeric `IntPtr` and `UIntPtr`

This new feature adds to the `nint` and `nuint` integral numeric types as aliases [System.IntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.intptr?view=net-6.0)` and `[System.UlntPtr](https://docs.microsoft.com/en-us/dotnet/api/system.uintptr?view=net-6.0).

The two types [nint](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#characteristics-of-the-integral-types) and [nuint](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#characteristics-of-the-integral-types) 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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#numeric-intptr-and-uintptr) 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](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#characteristics-of-the-integral-types):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9tliev9lu9tlh0dvbq7.png)
 

If you want to know more about this feature, I recommend you, as always, to consult the original source: [Microsoft Numeric IntPtr and UIntPtr](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#numeric-intptr-and-uintptr)

---

## Auto-default struct

This [C# 11 feature](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#auto-default-struct) 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](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct#struct-initialization-and-default-values):

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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#auto-default-struct), 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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#pattern-match-spanchar-or-readonlyspanchar-on-a-constant-string) opens the possibility to **use the same logic for pattern matching** [ReadOnlySpan<char>](https://docs.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay) and [Span<char>](https://docs.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay) variables.

If you want to know more about this feature, I recommend you, as always, to consult the original source: [Microsoft pattern matching](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#pattern-match-spanchar-or-readonlyspanchar-on-a-constant-string)

---

## Extended nameof scope

The function of `nameof` expressions is to [generate the name](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof) 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](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof):

[With the new version of C#](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#extended-nameof-scope) 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](https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#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?