Search code examples
c#visual-studiovisual-studio-2022

Is there a quick way to navigate to the underlying object of a variable in Visual Studio?


Let's take the example:

var myObject = GetCustomer(1234);

Where GetCustomer retrieves an object which is representative of a customer with the class myClass.

Is there a quick way in VS2022 to navigate to the declaration of myClass either through the mouse, keyboard or menu?

If I right-click on myObject, I get a number of items which pop up. I've tried "Go to Declaration", "Navigate To...", "Go to Definition", "Go to base", "Go to Implementation", none of them take me to where myObject is actually declared (not sure why "go to declaration" doesn't work, it takes me to the first time the variable is used in the current function).

As a bonus, I do have reSharper also installed, so perhaps a way to do it in there, but I don't see anything.


Solution

  • If the return type of GetCustomer is myClass (so the type of myObject is also myClass), you can navigate to myClass using either of these options:

    • Hold Ctrl and click the var keyword.
    • Right-click the var keyword and click Go to Definition.

    If GetCustomer might return a subclass of myClass, then you can right-click the var keyword and click Go to Implementation to see a list of subclasses of myClass.