Skip to content

Repo sync for protected branch #10365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/get-started/csharp/tutorial-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ The code block collapses to just the first line, followed by an ellipsis (`...`)

::: moniker range="<=vs-2019"

The Visual Studio editor makes it easy to inspect the definition of a type, method, etc. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** or pressing **F12** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-definition). Let's peek at the definition of the `string` type.
The Visual Studio editor makes it easy to inspect the definition of a type, method, etc. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** or pressing **F12** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-at-a-definition). Let's peek at the definition of the `string` type.

1. Right-click on any occurrence of `string` and choose **Peek Definition** from the content menu. Or, press **Alt**+**F12**.

Expand All @@ -217,7 +217,7 @@ The Visual Studio editor makes it easy to inspect the definition of a type, meth

::: moniker range=">=vs-2022"

The Visual Studio editor makes it easy to inspect the definition of a type, method, or variable. One way is to go to the definition, in whichever file has it, by choosing **Go to Definition** or pressing **F12** anywhere a symbol is referenced. An even quicker way that doesn't move your focus away from the code you're working on is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-definition).
The Visual Studio editor makes it easy to inspect the definition of a type, method, or variable. One way is to go to the definition, in whichever file has it, by choosing **Go to Definition** or pressing **F12** anywhere a symbol is referenced. An even quicker way that doesn't move your focus away from the code you're working on is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-at-a-definition).

Let's peek at the definition of the `string` type.

Expand Down
4 changes: 2 additions & 2 deletions docs/get-started/tutorial-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ The code block collapses to just the first line, followed by an ellipsis (`...`)

::: moniker range="<=vs-2019"

The Visual Studio editor makes it easy to inspect the definition of a type, method, etc. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../ide/go-to-and-peek-definition.md#peek-definition). Let's peek at the definition of the `string` type.
The Visual Studio editor makes it easy to inspect the definition of a type, method, etc. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../ide/go-to-and-peek-definition.md#peek-at-a-definition). Let's peek at the definition of the `string` type.

1. Right-click on any occurrence of `string` and choose **Peek Definition** from the content menu. Or, press **Alt**+**F12**.

Expand All @@ -261,7 +261,7 @@ The Visual Studio editor makes it easy to inspect the definition of a type, meth

::: moniker range=">=vs-2022"

The Visual Studio editor makes it easy to inspect the definition of a type, method, or variable. One way is to go to the definition, in whichever file has it, by choosing **Go to Definition** or by selecting the **F12** key anywhere a symbol is referenced. An even quicker way that doesn't move your focus away from the code you're working on is to use [Peek Definition](../ide/go-to-and-peek-definition.md#peek-definition).
The Visual Studio editor makes it easy to inspect the definition of a type, method, or variable. One way is to go to the definition, in whichever file has it, by choosing **Go to Definition** or by selecting the **F12** key anywhere a symbol is referenced. An even quicker way that doesn't move your focus away from the code you're working on is to use [Peek Definition](../ide/go-to-and-peek-definition.md#peek-at-a-definition).

Let's peek at the definition of the `string` type.

Expand Down
4 changes: 2 additions & 2 deletions docs/get-started/visual-basic/tutorial-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ The code block collapses to just the first line, followed by an ellipsis (`...`)

::: moniker range="<=vs-2019"

The Visual Studio editor makes it easy to inspect the definition of a type, method, etc. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-definition). Let's peek at the definition of the `String` type.
The Visual Studio editor makes it easy to inspect the definition of a type, method, etc. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-at-a-definition). Let's peek at the definition of the `String` type.

1. Right-click on the word `String` and choose **Peek Definition** from the content menu. Or, press **Alt**+**F12**.

Expand All @@ -212,7 +212,7 @@ The Visual Studio editor makes it easy to inspect the definition of a type, meth

::: moniker range=">=vs-2022"

The Visual Studio editor makes it easy to inspect the definition of a type or class member. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-definition). Let's peek at the definition of the `String` type.
The Visual Studio editor makes it easy to inspect the definition of a type or class member. One way is to navigate to the file that contains the definition, for example by choosing **Go to Definition** anywhere the symbol is referenced. An even quicker way that doesn't move your focus away from the file you're working in is to use [Peek Definition](../../ide/go-to-and-peek-definition.md#peek-at-a-definition). Let's peek at the definition of the `String` type.

1. Right-click on the word `String` and choose **Peek Definition** from the content menu. Or, press **Alt**+**F12**.

Expand Down
81 changes: 51 additions & 30 deletions docs/ide/go-to-and-peek-definition.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: View type definitions with Go To and Peek features
description: Explore the Go To Definition and Peek Definition features that enable you to easily view the definition of a type or member in Visual Studio.
ms.date: 01/10/2018
ms.topic: conceptual
ms.date: 08/21/2024
ms.topic: concept-article
helpviewer_keywords:
- code editor, view definition
- go to definition
Expand All @@ -13,64 +13,85 @@ author: ghogen
ms.author: ghogen
manager: mijacobs
ms.subservice: general-ide

#customer intent: As a developer, I want to use the Go To and Peek features in Visual Studio, so I can easily view type and member source definitions for my code.
---

# View type and member definitions

Developers often need to view the source code definitions for types or class members they use in their code. In Visual Studio, the **Go To Definition** and **Peek Definition** features enable you to easily view the definition of a type or member. If the source code is not available, metadata is displayed instead.
Developers often need to view the source code definitions for types or class members they use in their code. In Visual Studio, the **Go To Definition** and **Peek Definition** features let you easily view the definition of a type or member. If the source code isn't available, Visual Studio shows the metadata instead.

## Go To Definition
## Go to a definition

The **Go To Definition** feature navigates to the source of a type or member, and opens the result in a new tab. If you are a keyboard user, place your text cursor somewhere inside the symbol name and press **F12**. If you are a mouse user, either select **Go To Definition** from the right-click menu or use the **Ctrl-click** functionality described in the following section.
When you use the **Go To Definition** feature, Visual Studio locates the source code for the selected type or member. If the source code is in a different file, the file opens in a new window. Visual Studio changes the current focus to highlight the relevant entry.

### Ctrl-click Go To Definition
:::image type="content" source="./media/go-to-definition-new-window.png" border="false" alt-text="Screenshot that shows how Go To Definition opens the source file for the selected symbol in a new window." lightbox="./media/go-to-definition-new-window.png":::

**Ctrl**+**click** is a shortcut for mouse users to quickly access **Go To Definition**. Symbols become clickable when you press **Ctrl** and hover over the type or member. To quickly navigate to the definition of a symbol, press the **Ctrl** key and then click on it. It's that easy!
There are several ways you can access **Go To Definition**:

![Mouse click go to definition animation](../ide/media/click_gotodef.gif)
- Position the cursor inside the symbol name and use the **F12** function key

You can change the modifier key for mouse-click **Go To Definition** by going to **Tools** > **Options** > **Text Editor** > **General**, and selecting either **Alt** or **Ctrl**+**Alt** from the **Use modifier key** drop-down. You can also disable mouse-click **Go To Definition** by unchecking the **Enable mouse click to perform Go To Definition** checkbox.
- Right-click the symbol name and select **Go To Definition**

![Enabling mouse-click go to definition](../ide/media/editor_options_mouse_click_gotodef.png)
- Use the **Ctrl**+**Click** keyboard/mouse shortcut on the symbol name

## Peek Definition
### Change action shortcut

The **Peek Definition** feature lets you preview the definition of a type without leaving your current location in the editor. If you are a keyboard user, place your text cursor somewhere inside the type or member name and press **Alt + F12**. If you are a mouse user, you can select **Peek Definition** from the right-click menu.
You can enable or disable the **Ctrl**+**Click** shortcut for **Go To Definition** with the **Enable mouse click to perform Go To Definition** option under **Tools** > **Options** > **Text Editor** > **General**.

To enable **Ctrl**+**click** functionality, go to **Tools** > **Options** > **Text Editor** > **General**. Select the option **Open definition in peek view** and click **OK** to close the **Options** dialog box.
To change the modifier key for the shortcut, expand the **Use modifier key** dropdown list and select **Ctrl** (default), **Alt**, or **Ctrl**+**Alt**:

![Setting the mouse-click peek definition option](../ide/media/editor_options_peek_view.png)
:::image type="content" source="./media/editor-options-go-to-definition.png" border="false" alt-text="Screenshot of the Text Editor options for the Go To Definition feature, including the 'Use modifier key' setting.":::

Then, press **Ctrl** (or whichever modifier key is selected in **Options**), and click on the type or member.
## Peek at a definition

![Peek definition animation](../ide/media/peek_definition.gif)
The **Peek Definition** feature lets you preview the definition of a type without changing the focus from your current file in the editor. Visual Studio opens a condensed **Peek** view window for the source file in place with your current file. You can change the size of the **Peek** view window to see more of the source definition:

If you peek another definition from the popup window, you start a breadcrumb path that you can navigate using the circles and arrows that appear above the popup.
:::image type="content" source="./media/peek-definition-view-window.png" border="false" alt-text="Screenshot that shows how Peek Definition opens the source for the selected symbol in a peek view window within the current view." lightbox="./media/peek-definition-view-window.png":::

For more information, see [How to: View and edit code by using Peek Definition (Alt+F12)](how-to-view-and-edit-code-by-using-peek-definition-alt-plus-f12.md).
When you close the **Peek** view window, your current code file remains open in the editor.

## View metadata as source code (C#)
There are several ways you can access **Peek Definition**:

- Position the cursor inside the symbol name and select **Alt**+**F12** function key

- Right-click the symbol name and select **Peek Definition**

- Use the **Ctrl**+**Click** keyboard/mouse shortcut on the symbol name

### Change action shortcut

When you view the definition of C# types or members whose source code is not available, their metadata is displayed instead. You can see the declarations of the types and members, but not their implementations.
You can configure the **Ctrl**+**Click** keyboard/mouse shortcut for **Go To Definition** to open the source code file in **Peek** view. Use the **Open definition in peek view** option under **Tools** > **Options** > **Text Editor** > **General**.

When you run the **Go To Definition** or **Peek Definition** command for an item whose source code is unavailable, a tabbed document that contains a view of that item's metadata, displayed as source code, appears in the code editor. The name of the type, followed by **[from metadata]**, appears on the document's tab.
:::image type="content" source="./media/editor-options-peek-view.png" border="false" alt-text="Screenshot of the Text Editor options for the Peek Definition feature.":::

### Use breadcrumb path for multiple files

If you peek another definition from the open **Peek** view window, you start a breadcrumb path. You can peek forward and backward along the path by using the circles and arrows above the **Peek** view window:

:::image type="content" source="./media/peek-backward-breadcrumb.png" border="false" alt-text="Screenshot that shows the breadcrumb path of open source files for Peek Definition and the option to peek backward." lightbox="./media/peek-backward-breadcrumb.png":::

When you close the **Peek** view window, Visual Studio closes all open files in the breadcrumb path. For more information, see [View and edit code by using Peek Definition (Alt+F12)](how-to-view-and-edit-code-by-using-peek-definition-alt-plus-f12.md).

## View metadata as source code (C#)

For example, if you run the **Go To Definition** command for <xref:System.Console>, metadata for <xref:System.Console> appears in the code editor as C# source code. The code resembles its declaration, but does not show an implementation.
When you try to look at the definition for a C# type or member and the source code isn't available, Visual Studio shows metadata for the symbol instead. You can see the declarations of the types and members, but not their implementations. The tab label for the window includes the symbol name followed by **[from metadata]**:

![Metadata as Source](../ide/media/metadatasource.png)
:::image type="content" source="./media/view-source-metadata.png" border="false" alt-text="Screenshot that shows how Visual Studio displays metadata only when the source code for the selected symbol isn't available." lightbox="./media/view-source-metadata.png":::

> [!NOTE]
> When you try to run the **Go To Definition** or **Peek Definition** command for types or members that are marked as internal, Visual Studio does not display their metadata as source code, regardless of whether the referencing assembly is a friend or not.
> If you try to look at the definition for an *internal* type or member, Visual Studio doesn't display the metadata as source code, regardless of whether the referencing assembly is a *friend*. For more information, see [Friend assemblies](/dotnet/standard/assembly/friend).

### View decompiled source definitions instead of metadata (C#)
### View decompiled source definitions (C#)

You can set an option to see decompiled source code when you view the definition of a C# type or member whose source code is unavailable. To turn on this feature, choose **Tools** > **Options** from the menu bar. Then, expand **Text Editor** > **C#** > **Advanced**, and select **Enable navigation to decompiled sources**.
When the source code for the selected C# symbol isn't unavailable, you can set an option to see decompiled source definitions rather than metadata. Use the **Enable navigation to decompiled sources** option under **Tools** > **Options** > **Text Editor** > **C#** > **Advanced**.

![Viewing a decompiled definition](media/go-to-definition-decompiled-sources.png)
:::image type="content" source="./media/view-source-decompiled.png" border="false" alt-text="Screenshot that shows how Visual Studio displays decompiled source code when the definition for the selected symbol isn't available." lightbox="./media/view-source-decompiled.png":::

> [!NOTE]
> Visual Studio reconstructs method bodies using ILSpy decompilation. The first time you access this feature, you must agree to a legal disclaimer regarding software licensing and copyright and trademark laws.
> Visual Studio reconstructs method bodies by using [ILSpy](https://github.com/icsharpcode/ILSpy) decompilation. The first time you access this feature, you must agree to a legal disclaimer regarding software licensing and copyright and trademark laws.

## Related content

- [Navigate code](../ide/navigating-code.md)
- [How to: View and edit code by using Peek Definition (Alt+F12)](how-to-view-and-edit-code-by-using-peek-definition-alt-plus-f12.md)
- [Navigate your code in Visual Studio](../ide/navigating-code.md)
- [View and edit code by using Peek Definition (Alt+F12)](how-to-view-and-edit-code-by-using-peek-definition-alt-plus-f12.md)
Binary file removed docs/ide/media/click_gotodef.gif
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ide/media/editor-options-peek-view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified docs/ide/media/editor_options_peek_view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added docs/ide/media/go-to-definition-new-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/ide/media/metadatasource.png
Binary file not shown.
Binary file added docs/ide/media/peek-backward-breadcrumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ide/media/peek-definition-view-window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/ide/media/peek_definition.gif
Binary file not shown.
Binary file added docs/ide/media/view-source-decompiled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ide/media/view-source-metadata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/ide/media/vs2015_manage_subs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/ide/reference/options-text-editor-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Selected by default, this option enables the editor to suggest possible completi

## Click to peek in margin

The **Single click** setting is selected by default. The settings you choose from allow you to customize how Peek Definition works when you use it to display the differences between Git commits. You can change the default **Single click** setting to **Double click**, or you can select **None** to turn off the [Peek Difference UI](../../version-control/git-line-staging.md#peek-difference-support).
The **Single click** setting is selected by default. The settings you choose from allow you to customize how Peek Definition works when you use it to display the differences between Git commits. You can change the default **Single click** setting to **Double click**, or you can select **None** to turn off the [Peek Difference UI](../../version-control/git-line-staging.md#view-staged-changes-with-peek-difference).

::: moniker-end

Expand Down
Loading