ReSharper 2024.1 Help

Code Inspection: Join local variable declaration and assignment

Category

Common Practices and Code Improvements

ID

JoinDeclarationAndInitializer

EditorConfig

resharper_join_declaration_and_initializer_highlighting

Default severity

Suggestion

Language

C#

Requires SWA

No

If you declare a local variable and initialize it later without any conditions, ReSharper suggests joining the declaration and the assignment in the place where the variable is first initialized. This removes unnecessary line and improves readability of your code.

Here is an example of a quick-fix suggested by this inspection:

int Bar() { int myInt; //do something myInt = 3; // do something else return myInt - 1; }
int Bar() { //do something var myInt = 3; // do something else return myInt - 1; }

When ReSharper joins the declaration and the assignment of your variable, it will use var or an explicit type depending on your preferences.

Last modified: 15 April 2024