Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Resharper change var to explicit types?

Tags:

resharper

Is it possible to make Resharper's tool "Cleanup code" (in Visual Studio) to change all var to explicit types?

Example: I'd like this:

var person = new Person();

to be changed to this:

Person person = new Person();

Edit after suggested solutions:

Maybe I'm doing something wrong, but it doesn't work for me:

image showing how the suggested solution doesn't work

like image 848
FatAlbert Avatar asked May 23 '12 13:05

FatAlbert


People also ask

Does C# have explicit variable declaration?

You can set different preferences of using 'var' or explicit type for different types: For built-in types — applies to C# built-in types. For simple types — applies to types without generic parameters. Elsewhere — applies to generic types and deconstruction declarations.

Why you should use var c#?

By using “var”, you are giving full control of how a variable will be defined to someone else. You are depending on the C# compiler to determine the datatype of your local variable – not you. You are depending on the code inside the compiler – someone else's code outside of your code to determine your data type.

Does var affect performance C#?

Performance impact of using VAR in C#No, because at compile type it results in the same code as if you had explicitly typed your code.

What is var keyword in Java?

The var keyword was introduced in Java 10. Type inference is used in var keyword in which it detects automatically the datatype of a variable based on the surrounding context. The below examples explain where var is used and also where you can't use it. 1. We can declare any datatype with the var keyword.


2 Answers

You can set this up in the (Resharper) options under Code Cleanup. Just create a new profile/modify an existing one and then navigate to C#->Use 'var' in declaration. Set the "Replace direction" to "Can (change) 'var' to type usage" and "Local variable declaration style" to "Always use explicit type"

like image 182
Axel Wilczek Avatar answered Sep 22 '22 07:09

Axel Wilczek


Yes. In the "Code cleanup" tool, under "Use 'var' in declaration" set the "Replace direction" to "Can 'var' to type usage".

Creating Custom Profiles

like image 34
Sjoerd Avatar answered Sep 22 '22 07:09

Sjoerd