C# 11 is nearing completion. This article covers what's new in Visual Studio 17.3 and what we didn't cover in the April Update for Visual Studio 17.2 and the February Update for Visual Studio 17.1 .
The new features in this preview follow the three investment themes of C# 11:
Object initialization improvements : No matter what rules you want to enforce for mutable and immutable members, you can more easily support constructors and object initializers in types. Features include:
- Required member
-
ref
field
Generic math support : You can write algorithms once for many number types. These functions make statistics, machine learning, and other math-intensive applications easier using C# and .NET. Features include:
- Static abstract and static virtual members in interfaces
- Relax right shift requirements
- unsigned right shift operator
- Value
IntPtr]
- Developer Productivity: We've added more language features to increase your productivity.
New extended feature:nameof
.
The following sections outline each function and link in Microsoft Docs , where you can read more. To try out these features, you need to have preview enabled in your project. This is explained in the What's New in C# 11 article in the documentation.
Object initialization improvements
The required member allows you to write classes and struct types that require the caller to set certain properties. Refer to this Person
type:
public class Person
{
public string FirstName { get; init; }
public string LastName {get; init; }
}
The caller should use object initializers to set the values of the FirstName
and LastName
properties. But before Visual Studio 17.3, the compiler could not force the caller to have these properties set. A constructor that requires arguments is the only way to ensure that the user sets the FirstName
and LastName
properties. The required members communicate to the compiler and caller that they must set these properties. Add the required
modifier to the member declaration:
public class Person
{
public required string FirstName { get; init; }
public required string LastName {get; init; }
}
All callers must contain object initializers for the FirstName
and LastName
properties, otherwise the compiler will issue an error. The compiler informs the caller that there are required members that are not initialized. Developers must address this issue immediately.
If the Person
type was written for an earlier version and contains a constructor that sets properties, you can still use the required member. You should annotate any existing constructors with the SetsRequiredMembers
attribute:
public class Person
{
public required string FirstName { get; init; }
public required string LastName {get; init; }
[SetsRequiredMembers]
public Person(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
public Person() {}
}
SetsRequiredMembers
attribute indicates that the constructor sets all required members. The compiler knows to use
Person (string firstName, string lastName)
The caller of the constructor has set the required member. The parameterless constructor does not contain this property, so callers using this constructor must initialize all required members with an object initializer.
The above example uses properties, but you can also apply required members to field declarations.
This preview also includes initial implementations of ref fields and scoped values . These changes provide availability for the ref
field in the --- ref struct
type. You can also use the scoped
keyword to limit the lifetime of the ref
parameter. Feature proposals and updated changes now provide the best documentation for this feature. We found a few scenarios that required a language change to be safe to use. The updated changes will be available in a preview to be released at a later date, and the documentation will reflect the final design.
General Math Support
We added general math as a feature for motivating scenarios. You'll only use these functions directly in advanced scenarios, such as writing mathematical algorithms that work with many types of numbers. Otherwise, you will benefit indirectly because the runtime uses the following functions:
- Static abstract and static virtual members in interfaces
- Relax right shift requirements
- unsigned right shift operator
- NumberIntPtr
Adding static abstractions and virtual members to interfaces lays a lot of important groundwork for general mathematics. This feature allows interfaces to declare operators as well as other static methods. A class implementing an interface must provide an implementation of the 静态抽象
method just like any other method declared in the interface. The compiler resolves calls to 静态
methods (including operators) at compile time. There is no runtime dispatch mechanism like instance methods. The documentation provides more details on the specific language rules required to make this function work.
Other language features remove some of the differences in number types, making it easier to write general mathematical algorithms. The right shift operator no longer requires the second operand to be int
, but any integer type will do! nint
and nuint
are the synonyms of System.IntPtr
and System.UIntPtr
respectively. These keywords can be used in place of these types. In fact, the new analyzer prompts you to choose keywords instead of type names. Finally, the unsigned right shift operator ( >>>
) avoids the cast when you do an unsigned shift.
Taken together, these changes and others like the check operator support the generic math runtime changes. Language improvements mean that the runtime team can make improvements to all numeric types in .NET. You can also take advantage of these features when your types implement contracts using operators or other static methods.
developer productivity
nameof
operators can now be used with method parameters . This feature allows you to use the nameof
operator in the property declaration of a method, as shown in the following example:
[return:NotNullIfNotNull(nameof(url))]
string? GetTopLevelDomainFromFullUrl(string? url)
welcome
Please download the latest Visual Studio 2022 preview and install the .NET 7 preview, or you can install the .NET 7 latest preview separately. Once installed, you can try out the new features by creating or opening a C# project and setting LangVersion
Preview
.
This Visual Studio preview brings us one step closer to the full feature set of C# 11. We continue to invest in several themes in this release. We've made corrections based on the feedback you gave us. Now is a good time to download the preview to try out all the new features and give us your feedback. We are focusing on C# 11 and .NET 7 with final updates.
To learn more about what's new in C# 11, head over to the official documentation.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。