JetBrains Rider 2024.1 Help

Code Inspection: Use preferred style of 'new' expression when created type is not evident

Category

Syntax Style

ID

ArrangeObjectCreationWhenTypeNotEvident

EditorConfig

resharper_arrange_object_creation_when_type_not_evident_highlighting

Default severity

Hint

Language

C#

Requires SWA

No

Starting with C# 9.0, you can create objects with the target-typed new operator without explicit type specification when the type can be inferred, that is List<string> _myList = new(); instead of List<string> _myList = new List<string>();.

Depending on the context, the optional type specification can either clutter your code with redundant information or, on the contrary, improve the readability.

This inspection controls code style preferences for object creation expressions when the created type is not evident (for example, in return statements).

By default, JetBrains Rider suggests explicitly specifying the created type in non-evident cases:

JetBrains Rider syntax style inspection: Specify created type

If you prefer another style for the new operator in your code, you can change the corresponding preferences.

For more information, refer to Code Syntax Style: Object Creation ('new()' vs 'new T()')

Last modified: 10 April 2024