简体   繁体   中英

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.

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)

在此输入图像描述

  1. “Resharper” menu
  2. “Options"
  3. “Code Inspection”, then from “Inspection Severity”, find "Use Var keyword when possible” and set to “Do not show”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM