I suggest you ...

Make Visual Studio suggest explicit type instead of var when ToList is called

This suggestion is migrated to Developer Community. Please use below link to view the current status.
https://developercommunity.visualstudio.com/content/idea/351488/make-visual-studio-suggest-explicit-type-instead-o.html
Currently when the .editorconfig or the Visual Studio settings say that the code should prefer var when the type is apparent Visual Studio suggest that I change code like this

List<string> test = Enumerable.Range(1, 2).Select(i => i.ToString()).ToList();

I should use var because the type is apparent. It does this even when I have code like GetItems().ToList(); While calling ToList makes it apparent that the type is a List<T> it is not the List that I care about, it is the T. This is a clearly a case where if the type should be explicit (assuming the user set csharp_style_var_when_type_is_apparent = true). Please provide a fix to this behavior. In addition code like this

MessageProcessorBase messageProcessor = MessageProcessorBase.GetMessageProcessor(eventType);

Now triggers a suggestion to use var. While the type is indeed mentioned on the right I am calling a static method and it may have returned something else.

Please change this behavior or provide additional option for this corner cases so users can configure them as they see fit.

60 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Stilgar shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

1 comment

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base