News

A Peek at Latest C# 11 Features Coming in November with .NET 7

Arriving with .NET 7 in early November will be many new features in C# 11, the latest iteration of Microsoft's flagship programming language.

Yesterday, in highlighting new features for C# 11 that came with the recent release of Visual Studio 2022 v17.3, Microsoft focused on generic math, along with object initialization improvements via required members and ref fields.

Other new features including raw string literals, generic attributes and more were rolled out with VS 2022 v17.1 and VS 2022 v17.2.

C# 11 Work on GitHub
[Click on image for larger view.] C# 11 Work on GitHub (source: Microsoft).

"We tend if we can, we tend to take some of the bigger features and drip them out over multiple releases to make sure that we get the high-impact scenarios first, that we get feedback along the way and so on," explained Microsoft's Mads Torgersen in a video published earlier this month titled "C# 11: The Highlights."

Generic Math
Dripping out with the v17.3 release is generic math, which is supported by these language features:

  • static virtual members in interfaces
  • checked user defined operators
  • relaxed shift operators
  • unsigned right-shift operator

Generic math uses a new feature called static virtuals in interfaces that lets .NET developers leverage static APIs from generic code. For example, using operators on generic types lets developers constrain the input to number-like types, being no longer required to write or maintain many similar implementations in order to support multiple types. For another example, devs get access to all their favorite operators and can use them from generic contexts.

"This feature allows interfaces to declare operators, or other static methods," Microsoft's Bill Wagner said in an Aug. 22 blog post. "Classes that implement an interface must provide the implementation of static abstract methods, just like other methods declared in interfaces. The compiler resolves calls to static methods, including operators, at compile time. There's no runtime dispatch mechanism as there is with instance methods. The docs provide more details on the specific language rules required to make this feature work."

Wagner also noted, "You'll only use these features directly in advanced scenarios, such as writing mathematics algorithms that work on multiple number types."

That's exactly what Torgersen did in his video demo that featured a "numeric algorithm." He said: "We've got decent numeric support, but when it comes to abstraction over anything numeric, that's always been a bit of a problem." In running through his code demo, he noted "the point here is to have some numeric algorithm and then think about what would it look like if I were to take this numeric algorithm and abstract it to being applicable to more kinds of numbers rather than having to copy/paste my code for different kinds of numeric types and whatnot as you have to today."

Object Initialization Improvements
A big enhancement here comes via required members, which enable developers to write class and struct types that require callers to set certain properties. Coders can add a required modifier to properties and fields to ensure those values are initialized in constructors and callers.

"The required modifier indicates that the field or property it's applied to must be initialized by all constructors or using an object initializer," says Microsoft documentation. "Any expression that initializes a new instance of the type must initialize all required members."

Torgersen explained the feature in a demo scenario of coding a person class where he ditched the old method that required multiple constructors with different parameters in order to control how a person class could be constructed, rather using one default constructor and then using object initializers to create new objects.

"In many ways, this is a great experience both for maintaining the class hierarchies and for the end user of it, but there's one thing that constructors can't do, and that is it can't make me give values for certain properties," Torgersen said. "It's always optional. Everything is optional, and that's a problem because you don't want to leave the first name and the last name with a default value which is null, which actually goes against the non-nullable types. That's actually what this warning is about. It's like, 'hey, you are leaving null in there. Your constructors aren't initializing this.' "

In the demo, in trying out different things, Torgersen showed how a person's LastName property can end up being null if the developer forgets to provide a value. "What we really want is to be able to say, 'I promise that I will make the caller of the constructor initialize this property.' It is required, and that is what we're adding in C# 11 here. We're saying required properties are a thing. By saying it's required, we're shutting up the warning saying it might still be null. The compiler trusts that the caller is going to provide it, and indeed, it's going to make them by giving you an error until you do."

Wagner also used a person class as an example to explain required members in his post this week. "Callers should use object initializers to set the values of the FirstName and LastName property. But prior to 17.3, the compiler can't enforce that callers must set those properties. A constructor that requires parameters is the only way to ensure the user sets the FirstName and LastName properties. Required members communicates to the compiler and callers that they must set those properties."

All Features So Far
Here's the list of new C# 11 features introduced with the last few minor version releases of Visual Studio 2022:

To try out the new features, developers must remember to enable preview features in Visual Studio projects.

"This Visual Studio preview gets us closer to the complete feature set for C# 11," Wagner said. "We've continued to invest across multiple themes in this release. We've made corrections along the way based on the feedback you've already given us. Now is a great time to download the preview, try all the new features, and give us feedback. We're listening and making final updates for C# 11 and .NET 7."

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

Subscribe on YouTube