Is there a "Go To Variable Type Declaration" function for C# in Visual Studio 2015 or a free plugin which does it?
Asked Answered
S

4

16
void MagicalFunction(MagicalType magic)
{
    ...
    magic.Poof("something");
    ...
    var foo = magic.GetFoo();
    ...
}

Pressing the hotkey on variable magic would navigate to definition of type MagicalType.

Pressing the hotkey on foo would go to definition of type Foo which is not directly visible here because of type-inference.

Resharper plugin has this functionality (called Go To Type of Symbol) , but is there a built-in alternative or a free-extension that does this?

Spital answered 19/1, 2016 at 5:19 Comment(2)
Please let us know if you found a free solution. Still to this date VS2017 cannot apply Go to Type of Symbol if the names don't match. Example can be a parameter in a lambda. Thanks a lot !Prudence
Looks like Microsoft doesn't care about it and one can only hope for VS extension developers to implement it. Or become one whoever becomes sick and tired enough of lacking this one... Have anyone wrote about it at microsoft forum or git or whatever else is available to let them know about this need though?Tricuspid
D
13

Right click on the "var" keyword, select "Go to definition" from context menu, and it will take you to the type definition of the inferred type of the variable. I have some tools installed, like Productivity Power Tools which were mentioned, so not sure if this option is available through clean VS2015.

Edit:

You can also with cursor on the "var" keyword press Ctrl-F12 (Go to Implementation), if you prefer to use keyboard. Ref: https://www.youtube.com/watch?v=xWcQhF-1hxA

Dukie answered 23/2, 2017 at 7:38 Comment(6)
This is what I've been searching for! Thank you! FYI F12 works here as well.Normandnormandy
This is the only right answer to this question. It should be bubbled up to the top.Lindsey
@JeffSaremi no it is not. The user wanted to find out the underlying type of a symbol that doesn't have a var. Example the parameter in a .Select(product => product.ApplyDiscount()) method, where product has no var before it.Prudence
This is absolutely NOT the answer to the question. None of the answers so far have answered the question (some of them haven't even properly red the question to begin with). This one even goes so far as to suggest using MOUSE. Iulian at least gets it. Keyboard focus is on a "variable name" you press a hotkey and it goes to the type declaration of the said variable.Spital
What I'd really love to see is an option to have it navigate to "just my code". So "Go to definition" on var of var magicItems = new List<MagicalType>() wouldn't navigate to List<T> but rather MagicalType.Leonoreleonsis
It's not gonna help for a lambda parameter that has no type written, even var.Tricuspid
H
5

Sort of.

You could press F12 on "magic" to get to its definition, and then F12 again to get to its class.

Also, while you can't get anything meaningful by F12 on "foo" since it would just highlight the line you're already on, if you F12 from the "var" immediately prior to foo, it will jump you to the Foo class, even though that type is being inferred.

Hawse answered 11/10, 2016 at 23:28 Comment(2)
I already use this double 'Go To Definition' (default key: F12) approach all the time. Not a solution.Spital
@Hawse how to you make a Go to Definition on a lambda parameter ? I think this is not the solution !Prudence
M
5

The function you are looking for is "Navigate To".

For some weird reason, the keyboard shortcut was removed from VS 2015. You can see this by going into the Edit menu. The option will be there but has no shortcut assigned to it.

You can fix this manually. The default was:

CTRL + ,

Go to Tools > Options > Environment > Keyboard, search for "Edit.NavigateTo" and reassign the shortcut. You can then place the cursor on the variable and hit the shortcut and a tiny window will overlay on the top right hand corner with possible candidates, one of them being the type definition.

Mcroberts answered 17/10, 2016 at 16:11 Comment(5)
Please read the question. I already use 'Navigate To' (CTRL + ,), it really is not the functionality I'm asking for here. What I'm looking for is something like this: jetbrains.com/help/resharper/2016.1/… (Go To Type of Symbol)Spital
I understand what you are looking for. I also use ReSharper. If you read the last part of my answer you will see that the window that pops up gives you what you are looking for (among other things). As far as I know, there is no other way to get that functionality. Please try it at least and see for yourself.Mcroberts
It doesn't include type of the variable(the symbol) in the list (unless the name of the variable is the same as the name of the type). I double checked it. :( It's useful for navigating to type by name (if you enter it manually) and that's what I use it for most of the time.Spital
Where is Edit.NavigateTo in VS 2017?Normandnormandy
No it's not working. I am using VS 2017 and I am trying to let Resharper go. Even 2017 has no Go to Type of Symbol. If the name is the same as the underlying type , your suggestion only makes a simple search (and of course it will be found this way), and not a Type-resolve. Funny enough is that the tool tip recognizes the type no matter the name. It just can't navigate to it !!Prudence
F
-1

Productivity Power Tools has a feature "Ctrl + Click Go To Definition This extension gives the editor a web browser by adding clickable hyperlinks to symbols in your code as you hold down the Ctrl key."

https://visualstudiogallery.msdn.microsoft.com/d0d33361-18e2-46c0-8ff2-4adea1e34fef

it deosn't work in the first case but it works very well when you ctrl click "var" in the second case

Firehouse answered 15/10, 2016 at 19:34 Comment(1)
1) "Go To Definition" is NOT the same as 2) "Go To Variable Type Definition". 1) goes to definition of the symbol, 2) goes to the definition of the TYPE of the symbol.Spital

© 2022 - 2024 — McMap. All rights reserved.