Get Up To Date with the Latest Features of C# 11
Tags: csharp, dotnet, programming, news
Published: 2022-09-20 13:30:42

Vacations are over for many people! Although it seems that Microsoft does not know what that is and they have just released **one of the last official previews of C# 11.** The development of the eleventh version of its programming language is in the final stages and Microsoft has decided to step on the gas by releasing new features.

These features have been presented by [Bill Wagner](https://twitter.com/billwagner?lang=es) — Software Developer at Microsoft, and on this occasion he has explained in detail their uses. Let’s go straight to it!

## Improved object initialization

The first feature is an improvement of **object initializers** and **constructors**. Now you will be able to support them in a much simpler and independent way.

This improvement in object initialization is mainly divided into two parts: **requied members** and `**ref**` **fields**. Let’s analyze both of them separately.

## Required members

The `required` modifier is the first of the enhancements of this feature over C# 11. The main function of this modifier is to tell the object initializer or constructors which properties or fields should be initialized.

According to Bill Wagner in the [last C# 11 Preview](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#improved-object-initialization):

> “[Required members](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/required) let you write class and struct types that _require_ callers to set certain properties.”

As usual, we rely on the original example provided:

{% embed https://gist.github.com/lgolubyev/892ef8d77a73fd11f070aa264dde2751 %}

[Based on this example](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#improved-object-initialization), Bill comments:

> “Callers should use object initializers to set the values of the `FirstName` and `LastName` property.”

One of the points to note is that in order for (_based on the previous example_), a user to set the `FirstName` and `LastName` properties, it is necessary that the costructor requires them.

To prevent the compiler from returning an error saying that the “**_required members weren’t initialized_**”, simply use the `required` modifier:

{% embed https://gist.github.com/lgolubyev/a1f463a9ee9b6ef62cc2d74f05197726 %}

As I said at the beginning, this is a feature released in this latest version of C# 11 but, what if the type was written in a previous version? Well, don’t worry, Microsoft has also thought about it and has the solution.

In the case of having written the type in a previous version, we must set the existing constructors using the `[SetRequiredMembers](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required)` attribute:

{% embed https://gist.github.com/lgolubyev/5dbc21e00143090e4ec34bb7c6a98bd6 %}

[Microsoft has also added](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#improved-object-initialization) that `scoped` values are now added next to `ref` fields and now, `ref` fields in `ref struct` types are possible:

> “You can also use the `scoped` keyword to limit the lifetime of `ref` parameters.”

If you want to know more about this feature, I recommend you, as always, to consult the original source: [Improved object initialization](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#improved-object-initialization)

## Generic math support

In this next feature Microsoft focuses on generic math and adds s**everal more advanced features** (_within this feature_). The only thing that [Microsoft comments](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#generic-math-support) about these advanced features is that it **is not common to use them in normal or usual scenarios**, but they have a focus towards scenarios of mathematical algorithms of multiple types of numbers.

Even if at this moment you think that you won’t be able to take advantage of this feature, [Microsoft says](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#generic-math-support):

> “…you’ll benefit indirectly because the runtime uses these features.”

These are the features Microsoft is referring to:

-   [Static abstract and static virtual members in interfaces](https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-11#generic-math-support)
-   [Relaxed right-shift requirements](https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-11#generic-math-support)
-   [Unsigned right shift operator](https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-11#generic-math-support)
-   [Numeric](https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-11#numeric-intptr-and-uintptr) `[IntPtr](https://docs.microsoft.com/dotnet/csharp/whats-new/csharp-11#numeric-intptr-and-uintptr)`

Going back to generic math support, the new thing about this is the abstract and static virtual members in the interfaces. Now you can **declare a static method via an interface.** However, Microsoft warns that it is necessary to provide the implementations of these methods through the classes that implement them.

We also have other [**minor improvements**](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#generic-math-support) such as:

-   The second operand of a shift operator (>>) no longer needs to be `int`.
-   You can now use `nuint`, which is the shortened option of `System.UIntPtr`.
-   You can also use `nint`, which is the shortened option of `System.IntPtr`.
-   You will be able to avoid shifts when unsigned shifts have to be made by using unsigned right-shift operator (>>>).

If you want to know more about these features, I recommend you, as always, to consult the original source: [Generic math support](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#generic-math-support)

## Nameof operator mprovement

Last but not least, there is a [developer productivity improvement](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#developer-productivity). This time we focus on the `nameof` operator, which now has the ability to be used with method parameters.

This way you will be able to use `nameof` inside method attribute declarations. As you can’t miss it, [Microsoft provides us with this example](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#developer-productivity):

{% embed https://gist.github.com/lgolubyev/68566a15a7addac8c1784345e2094ed9 %}

If you want to know more about this feature, I recommend you, as always, to consult the original source: [Developer productivity](https://devblogs.microsoft.com/dotnet/csharp-11-preview-august-update/#developer-productivity)

Little else can be gleaned from this latest preview. C# 11 is just around the corner and there are only 2 months left before its official release in November. What else can we expect from Microsoft? Who knows?

If you have been disconnected during these weeks, you may be interested in consulting the previous release, which was released this summer:

{% embed https://dev.to/bytehide/5-new-c-11-features-you-might-have-missed-32d8 %}

If you want to be among the first to be updated and know all the news about C# and .NET remember to follow me so you don’t miss anything!