.NET Tools
Essential productivity kit for .NET and game developers
Extended C# Support in ReSharper and Rider 2023.2 EAP 9
We’ve just published new EAP builds for both ReSharper 2023.2 and Rider 2023.2. Both contain updates to C# support. Let’s dive in!
C#12 preview support
Primary constructors
In the 2023.2 version, we introduced primary constructor support for non-record
classes
and structs
from the C# 12 preview. Unlike primary constructors in records
, primary constructors in classes
and structs
do not implicitly declare members such as properties or fields. Instead, you can use the primary constructor parameters all over the type declaration. The compiler will figure out how to make the parameter value available in the context of usage. This may greatly reduce the amount of code repetitions.
While the ability to make use of primary parameters in all instance members of type declarations is powerful, some of you may find this implicit “capture” of primary parameters into a type’s state a bit confusing. To make things more intuitive, we introduced additional coloring for “captured” primary constructor parameters. The default color is now the same as the ordinary fields’ colors. As a result, it helps you easily differentiate ordinary method parameters from primary constructor parameters in the parameter list of the type.
The ReSharper and Rider 2023.2 EAP builds can now detect those types where the initialization can be expressed with new primary constructors. It can also suggest if private fields may be completely replaced by the use of primary constructor parameters. C# 12 now also supports the ability to add attributes for primary constructors (in class
, record
, and struct
) by using the method:
attribute target. The corresponding quick-fix to use the primary constructor will handle everything for you.
Sometimes, the initialization of the type must be made less trivial and can no longer be expressed using a primary constructor. For such cases, we introduced context actions to convert the C# 12 primary constructors back to ordinary ones.
As usual, you can also do a reverse replacement from primary constructor parameters to ordinary fields if you need to make a captured state available for derived classes with protected fields or if you need to annotate a field with an attribute.
Other updates
Besides the primary constructors for non-record
classes
and structs
, we added support for one more feature from the C# 12 preview.
You can now use ;
instead of the empty { }
block for type declarations for class
, struct
, interface
, and even enum
declarations. The ReSharper and Rider 2023.2 EAP builds suggest you use this new syntax. You can then apply the quick-fix for different scopes including file, folder, project, and the entire solution.
Support for the IAsyncDisposable
interface
In addition to a few inspections to catch an early disposal of using
variables, which we introduced in the EAP 5 build, we improved the Generate dispose pattern feature to support the IAsyncDisposable
interface and be able to generate an async method to release the resources.
Better C# discards support
Both ReSharper and Rider 2023.2 EAPs received a bunch of new useful additions to C# discard support. As you may know, discards are placeholder variables that are intentionally unused in application code.
You can now quickly check names and types of discarded values with Push-to-Hint functionality. Press and hold the Ctrl key to activate the Push-to-hint mode.
Since _
is a valid name of an identifier in C#, we introduced some inspections to reduce the possibility of confusing _
as a discard symbol. It can prepare your code for upcoming breaking changes in C#.
There is now a warning about variables and parameters named _
that are actually being used. They look like a “discarded” value, but in reality, they represent very much the opposite. To clarify the code, we now offer the quick-fix to rename the lambda parameter.
Also, we detect older code patterns that existed before the discard assignment was introduced in C# and offer the quick-fix – Use discard assignment – to rewrite the code in a more idiomatic way using discards.
That’s it for today!
The full list of improvements and fixes can be found on YouTrack:
If you have something to share with us, don’t hesitate to let us know in the comments section below.