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:
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.
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.
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.
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.
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"
Yes. In the "Code cleanup" tool, under "Use 'var' in declaration" set the "Replace direction" to "Can 'var' to type usage".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With