See the type of a var in Visual Studio
Asked Answered
L

9

13

Is there a way to see the type of a var within the Visual Studio 2013 code editor?

When I have a crazy linq query it would be nice to see what the resulting type will be. I don't want to replace the var keyword with the actual type, I just want to see what it is.

Laguna answered 1/10, 2014 at 8:48 Comment(4)
by hovering on particular var??Transgress
In some versions, mouseover the var keyword will not show anything, but in 2013 it should show the Type. You can also mouseover the variable itself.Extempore
How to: Use Debugger Variable WindowsInformer
@TimSchmelter - Not sure if your link applies. I want to see the type during design time.Laguna
T
9

You can get it by Hovering on particular var keyword.

For example see in below image the tooltip shows the details.

enter image description here

Transgress answered 1/10, 2014 at 8:52 Comment(1)
Does anyone know how to get this to work while debugging? When debugging I can't hover over fields/methods to see their descriptions, but CTRL-K, I works when debugging.Nottinghamshire
C
18

There is also a keyboard shortcut that will show you. Put the cursor over var and type:

ctrl+k, i

enter image description here

Cirone answered 1/10, 2014 at 8:55 Comment(3)
That's exactly what I was looking for!Resistencia
Great ...but how did u come to know this ?Carnay
Don't recall exactly where I found it, possibly from digging through the VS Keyboard mappingsCirone
K
16

For newer versions of VS (I'm not sure this applies to 2013), you have the option to go down to:

Tools > Options > Text Editor > C# > Advanced

Scroll down to the "Inline Hints' section and enable Display inline type hints to show type hints inline with the line where the var is specified. There's also an option to display parameter name hints there, also very useful.

Kunin answered 20/9, 2022 at 11:41 Comment(0)
T
9

You can get it by Hovering on particular var keyword.

For example see in below image the tooltip shows the details.

enter image description here

Transgress answered 1/10, 2014 at 8:52 Comment(1)
Does anyone know how to get this to work while debugging? When debugging I can't hover over fields/methods to see their descriptions, but CTRL-K, I works when debugging.Nottinghamshire
A
6

enter image description here For Vs 2019 or 2022 Go to visual studio Option > Text Editor > c# > Advanced > Display inline type hints

enter image description here

Afforest answered 31/12, 2022 at 8:13 Comment(0)
O
3

Hover your mouse over the var keyword, it will show it in a tooltip.

Ossa answered 1/10, 2014 at 8:51 Comment(3)
You can also use F12 to go to definition on the var keyword, and it'll take you to the class.Psychobiology
Will it do this for the result of a linq query?Laguna
Yes, the compiler can still infer the type from a linq query - unless you select an anonymous type, I'm not sure what it does then.Ossa
H
2

Hover on the variable... It will tell you.

Hick answered 1/10, 2014 at 8:51 Comment(2)
variable? or var keyword?Fearful
Both will work. On the Var keyword, it will tell you the Type var replace, on the variable, it will let you explore it as a usual variable (including the type).Hick
L
2

The text of the type can often be obtained (using copy and paste) by temporarily changing the var to an explicit type, say int. That will force a compilation error. After doing a Build solution the type of the variable should be written in both the Error list and the Output panes.

Libre answered 2/10, 2014 at 10:26 Comment(0)
B
1

Move your mouse cursor hover the var keyword, a tooltip will show you the actual type.

Burma answered 1/10, 2014 at 8:51 Comment(0)
C
0

Add a variable watch. Check the type in the watch window.

Carouse answered 3/4, 2017 at 5:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.