I suggest you ...

Add QuickAction to refactor var to implicit type

I regularly type var as a time saver but would prefer the strong type itself to be in code.
So I type:
var result = SomeMethod();
I would like to do a quick action on var to convert it to:
List<SomeType> result = SomeMethod();

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

4 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Vlad commented  ·   ·  Flag as inappropriate

    It would be nice if var to type conversion was available as a quick action. My main reason to wish for that is a situation when I need to move var definition out of original scope, thus removing the implicit type information. Here is an example:
    if (something)
    {
    var a = this.Foo(); // type of 'a' is implied by Foo
    }

    If I need top refactor this code to expose 'a' outside of the current scope, in most cases I have to use explicit type definition:
    SomeClass a;
    if (something)
    {
    a = this.Foo();
    }

    // use 'a' here...

    It would be very nice if there was a quick way to convert 'var' into type.

  • AdminVisual Studio Team (Product Team, Microsoft Visual Studio) commented  ·   ·  Flag as inappropriate

    You can do something very close to what you're asking for in Visual Studio 2017 via the Code Style Configuration system (see Kasey Uhlenhuth's blog post at https://blogs.msdn.microsoft.com/dotnet/2016/12/15/code-style-configuration-in-the-vs2017-rc-update/ that talks about how to set things up) - if you set your preference for the various rules that relate to var (like csharp_style_var_for_built_in_types ) to be false:suggestion, you'll see suggestion dots under code that uses var, and can use a quick action to switch to the strong type.

    Hope this helps,
    Mark Wilson-Thomas
    Program Manager, Visual Studio IDE Team

  • Teofil Cobzaru commented  ·   ·  Flag as inappropriate

    It would be nice if the quick action (or something similar) appears as soon as you type in the method, so you can apply it right away, without moving the cursor back to var.

  • Wouter van de Ven commented  ·   ·  Flag as inappropriate

    I had exactly the same idea, now I find myself writing var a later looking up with F12 what type the function I'm calling is returning, then changing the var, because I believe that having explicit types makes to code more readable. Having this auto refactor would make me more productive.

Feedback and Knowledge Base