Get Up To Date with the Latest Features of C# 11

bytehide

ByteHide

Posted on September 20, 2022

Get Up To Date with the Latest Features of C# 11

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 — 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:

Required members let you write class and struct types that require callers to set certain properties.”

As usual, we rely on the original example provided:

Based on this example, 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:

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:

Microsoft has also added 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

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 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:

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

These are the features Microsoft is referring to:

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 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

Nameof operator mprovement

Last but not least, there is a developer productivity improvement. 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:

If you want to know more about this feature, I recommend you, as always, to consult the original source: 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:

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!

💖 💪 🙅 🚩
bytehide
ByteHide

Posted on September 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related