The reason that ReSharper is suggesting you use var
is that it is by default configured to do so.
Go into the options for ReSharper, find the "Inspection Severity" settings under "Code Inspection", and a ways down into that list, find the setting labelled "Use 'var' keyword when possible".
ReSharper will, with the default setting of "Show as suggestion", by default suggest you use var
instead of the actual type if:
- You're constructing an object (ie.
Type x = new Type(...);
)
- You're initializing it with a constant (ie.
int x = 5;
)
In both of these cases, ReSharper is programmed to assume that you can easily spot the correct type anyhow, so you can use var
.
If you don't want that, change that setting. You can also change that setting directly in the dropdown you get from Alt+Enter, just pick the bottom item in the dropdown menu:
and it will pop open a dialog asking how to treat that setting:
Also note that if you ask ReSharper to change the code to using var
, it won't suggest you turn it back, but you can, by hitting Alt+Enter again and picking the option "Specify type explicitly".
var
" -usesvar
- "No, no no, useint
!" -usesint
- "No, usevar
!" – Filature