Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper custom pattern var

The code inspection of Resharper suggests to use var in C# instead of explicit type pretty much everywhere. I don't like that option because too much var makes things unclear so I've disabled that option. However where i do like to use var is in cases of initializations with two times the type on the same line with generics (so in similar situations as the diamond operator from java 7), like:

Dictionary<string string> dic = new Dictionary<string, string>();
// I want a suggestion to replace this to
var dic = new Dictionary<string, string>();
// but I don't want to replace things like this:
Person p = new Person();

I made a custom pattern in Resharper:

Dictionary<$type1$, $type2$> $id$ = new Dictionary<$type1$, $type2$>();

with a replace to:

var $id$ = new Dictionary<$type1$, $type2$>();

This works fine, but the pattern also finds lines that are already converted with the rule. Why and how do I fix that?

Edit: put part of the text in bold because nobody appears to read it.

like image 959
user968698 Avatar asked Aug 21 '14 07:08

user968698


1 Answers

Resharper has 2 code inspections for the 'var' keyword. Go to Resharper->Options...->Code Inspection->Inspection Severity and change the 'Use 'var' keyword when possible' to a lower severity. (Note, on the Inspection Severity screen you can search for words in the text box at the top)

enter image description here

like image 185
Piers Myers Avatar answered Oct 08 '22 02:10

Piers Myers