30

Possible Duplicate:
Tool to refactor C# var to explicit type

Does Visual Studio have any type of shortcut (shortcut meaning short of writing out the actual type name) that allows you to write "var" and have it converted to the actual type name?

ex:

var x = new Dictionary<string, string>();

::Do magic thing::

Dictionary<string, string> x = new Dictionary<string, string>();

EDIT -- To all the question haters: I asked because I was in a situation where I wanted to find all references to a particular type in my solution. Vars don't show up in that search.

Community
  • 1
  • 1
carlbenson
  • 3,177
  • 5
  • 35
  • 54
  • 11
    ReSharper can do this. Not sure if you want to hear that :) –  Feb 01 '12 at 19:06
  • 1
    I don't think VS has it, but ReSharper certainly does. – Daniel Mann Feb 01 '12 at 19:06
  • 18
    Not everyone likes `var` and thats okay. – SomeWritesReserved Feb 01 '12 at 19:06
  • 2
    Why do you want this when `var` is perfectly acceptable? – Bernard Feb 01 '12 at 19:07
  • 8
    The non-var version hurts my eyes! – Meta-Knight Feb 01 '12 at 19:09
  • 8
    @Bernard Because it's a matter of preference? – Marlon Feb 01 '12 at 19:09
  • 1
    Are you going to remove all your `using` statements while you're at it, and expand out to `System.Collections.Generic.Dictionary x`? – Yuck Feb 01 '12 at 19:11
  • 2
    @Yuck I was hoping more along the lines of `global::System.Collections.Generic.Dictionary x`. – Marlon Feb 01 '12 at 19:14
  • Maybe dynamic Type will be usefull for your goal ? – TwTw Feb 01 '12 at 19:14
  • 7
    @Yuck, Marlon: `global::System.Collections.Generic.Dictionary x` – BoltClock Feb 01 '12 at 19:14
  • Another possibility: http://stackoverflow.com/questions/8402990/vs2008-replace-var-with-inferred-type – Anthony Pegram Feb 01 '12 at 19:14
  • @BoltClock I can't believe I didn't think of that when I wrote my comment! Nice one :D – Marlon Feb 01 '12 at 19:15
  • 2
    @BoltClock For that matter, `x` isn't descriptive enough. `global::System.Collections.Generic.Dictionary localVariableXOfTypeSystemCollectionsGenericDictionaryOfStringString;` – Yuck Feb 01 '12 at 19:17
  • 1
    "[...]brevity is the soul of wit[...]" - Polonius, _Hamlet_ – canon Feb 01 '12 at 19:17
  • 3
    I could understand not liking vars in some cases. I hate using vars for ints, strings, bools, etc for(var i = 0;...) looks ugly. – corylulu Feb 01 '12 at 19:31
  • @Carl I don't think there were any *haters* here. People just got into a small joke with the comments; the close votes were because this has been asked before, and it's better for site content to link it that way. If there were down votes, don't worry about those at all. – Andrew Barber Feb 01 '12 at 20:44
  • @AndrewBarber, I meant "haters" lightheartedly. But, that being said, your response may be one of the the most considerate/friendly comments I've ever read on this site. Thank you! – carlbenson Feb 01 '12 at 20:51
  • @Carl You're welcome! It's sort of funny, as I'm actually in a generally grumpy mood today! hehe – Andrew Barber Feb 01 '12 at 21:10
  • Kind of an aside, but I would guess `var` became unintentionally popular when it was originally only meant for anonymous types; I would guess that if Microsoft would have known that it would become a "hey I can save typing now" shortcut, they would have put that in the IDE as a UI setting that only changed the way code was presented (e.g.,, "Show/Hide explicit types" in preferences). Would be nice if this were a per-user editor setting, but then again I still wish tabs would have won out over spaces. – jrh Feb 27 '19 at 14:50

1 Answers1

2

Third party tools such as Resharper can convert implicitly typed variables to explicitly typed, and vice versa. Resharper lets you massively convert all of them or pick and choose which instances to convert.

But let me direct you to Will using 'var' affect performance?, where the arguments for and against implicitly typing are made.

Community
  • 1
  • 1
Devin Burke
  • 13,642
  • 12
  • 55
  • 82
  • Note that it's possible to convert var to explicit type using (vanilla) Visual Studio (I tested this in Visual Studio 2017), see the question marked as a duplicate of this post. – jrh Mar 12 '19 at 14:26
  • Refactoring to replace var with an explicit type https://learn.microsoft.com/en-us/visualstudio/ide/reference/convert-var-to-explicit-type – Kevin Xiong Sep 17 '21 at 05:37