Compartilhar via


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

  • Anonymous
    June 04, 2007
    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.

  • Anonymous
    June 04, 2007
    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.

  • Anonymous
    June 20, 2007
    Welcome to the XXVIII Community Convergence. In these posts I try to wrap up events that have occurred

  • Anonymous
    March 27, 2008
    Does not work for me, I get the error "cannot navigate to" any ideas

  • Anonymous
    August 14, 2008
    It does not work for me. Nothing happen when I try Ctrl-shift-7 or -8

  • Anonymous
    October 15, 2008
    Thanks for the tip on how to "go back" from an F12.

  • Anonymous
    December 30, 2008
    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.

  • Anonymous
    February 23, 2009
    Is there any way to make it skip the metadata and go straight to the code?

  • Anonymous
    February 26, 2009
    Does not work for me, I get the error "cannot navigate to" any ideas

  • Anonymous
    March 27, 2009
    vb.net 2008 f12: go to definition ctrl+shft+f12 returns to prior location

  • Anonymous
    April 28, 2009
    The comment has been removed

  • Anonymous
    May 02, 2009
    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.

  • Anonymous
    March 12, 2010
    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. TIA

  • Anonymous
    March 13, 2010
    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.

  • Anonymous
    April 05, 2010
    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.

  • Anonymous
    September 17, 2010
    @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.

  • Anonymous
    May 04, 2012
    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.