F12 – Go To Definition

A few months ago when Bill Gates was on the Daily Show, Jon Stewart asked him a particularly insightful software question:

"So...what does the F12 button do?  Does it do anything?  Is it a joke button".  To which Bill replied jokingly - "I'd stay away from it if I were you." 

If you use Visual Studio though - I recommend you ignore Bill's advice here.  F12 is a quite useful keyboard shortcut.

Go To Definition

F12 itself invokes the "Go To Definition" (GTD) command.  This works on types, members, locals, range variables, etc.  It uses all the compiler's internal understanding of the code to make sure it takes you to the right member - even when there are many overloads or if your code is using a lot of type inference.

Metadata As Source

One of the additions to "Go To Definition" support in Visual Studio 2005 was something we call "Metadata as Source".  If you go to definition on a type which is defined in a referenced assembly - you will be presented with a "header file"-style view of the corresponding type and it's members.  This keeps you in the editor - and gives a convenient view of the type.  You can continue to navigate other members/types directly from the Metadata As Source view by using GTD again on those tokens.

Backward and Forward navigation

If you use GTD a lot, you'll probably find yourself wanting to go "back" to where you invoked "go to definition" from.  It turns out there is a keybinding for this:

  • ctrl-shift-8: Takes you back in the GTD stack, to the the last place you invoked GTD
  • ctrl-shift-7:  Takes you forward in the GTD stack, to the last place you invoked back
Code Definition Window

The Code Definition Window is toolwindow which shows a live GTD of whatever symbol is currently under the cursor in the editor.  If you find yourself using GTD a lot, you may want to pop up the Code Definition Window with ctrl-w-d.

BTW - If you are looking for more useful keyboard shortcuts using the C# Profile in Visual Studio - take a look at  the C# keybinding posters Karen blogged about a little while ago.

Comments (17)

  1. evarlast says:

    Thanks tons for this!

    I use F12 a lot, but I typically use ctrl-minus to go to last position. I did not know about ctrl-shift-7 and 8. I’ll have to try those out.

    I think F12 and ctrl-minus are two HUGE productivity keystrokes for developers.

  2. jstangroome says:

    Been using F12 for a long time in Visual Studio but was disappointed after installing the highly recommended ReSharper that it hijacks F12 for it’s own, less useful, purposes. One step forward, two steps back.

  3. Welcome to the XXVIII Community Convergence. In these posts I try to wrap up events that have occurred

  4. Juan says:

    Does not work for me, I get the error "cannot navigate to" any ideas

  5. haha_254 says:

    It does not work for me. Nothing happen when I try Ctrl-shift-7 or -8

  6. Abhijit says:

    Thanks for the tip on how to "go back" from an F12.

  7. xyzq says:

    I prefer Borland style like Go to Definition by Ctrl + Left button click. Its pretty practical to use.

    It could be implemented in VS too, i miss it.

  8. Is there any way to make it skip the metadata and go straight to the code?

  9. sudhakar says:

    Does not work for me, I get the error "cannot navigate to" any ideas

  10. Ken Jett says:

    vb.net 2008

    f12: go to definition

    ctrl+shft+f12 returns to prior location

  11. xhanness says:

    VS sucks – compare it to Eclipse – that is the best IDE

  12. A Bunch of thanks for F12. I searched a lot for it in VS.NET itself ut finally I got it from your post.

    Thank you again.

  13. Ken says:

    Any idea how to give GTD a source path so it would show the source code instead of metadata?  I can’t find that information in MSDN.


  14. Ken – There is not currently any way to point GTD at a source directory to use that instead of metadata.  The choice of which to use is based just on whether you have an assembly reference or project reference.  If you’d like to see a future version of Visual Studio support this, consider opening a suggestion at https://connect.microsoft.com/VisualStudio.

  15. leo says:

    I used visual studio 2008,It does not work for me,but,i change the options after, it work find.

    Tool–Options–Environment–Keyboard–Apply the following additonal keyboard mapping scheme:–default –OK

    have a good lucky.

  16. Kay says:

    @Ken   I had a similar problem with regards to f12 showing metadata.  I followed LukeH's comment below re assembly or project reference.  I deleted the referenced assembly and then added it as a project reference – It now works as expected.

  17. Fernando says:

    If your keyboard has a “Function Lock”, “F Lock” or “Fn” key – make sure it is pressed prior to trying to use the F12 key.

Skip to main content