Skip to content

Commit f3f6fa4

Browse files
Merge pull request #10180 from MicrosoftDocs/main638479695457126698sync_temp
For protected branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents 9ac47a4 + f4084e3 commit f3f6fa4

File tree

2 files changed

+56
-59
lines changed

2 files changed

+56
-59
lines changed

docs/ide/code-snippets.md

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Create and reuse code block snippets
3-
description: Explore code snippets, which are small blocks of reusable code that you can insert into a code file, such as `try-finally` or `if-else` blocks, classes, or methods.
4-
ms.date: 03/29/2024
2+
title: Understand and use code snippets
3+
description: Explore code snippets, which are small blocks of reusable code you can insert into a code file, and learn how to find and use them.
4+
ms.date: 04/05/2024
55
ms.topic: conceptual
66
f1_keywords:
77
- vs.ExpansionManagerImport
@@ -19,36 +19,44 @@ dev_langs:
1919
- CPP
2020
---
2121

22-
# Code snippets: what they are and how to add one
22+
# Code snippets and how to use them
2323

24-
Code snippets are small blocks of reusable code that you can add to a code file. Snippets most commonly contain often-used code blocks such as `try-finally` or `if-else` blocks. However, you can also use them to add entire classes or methods. Your snippets can even contain replacement parameters, which act as placeholders. Snippets are available for many languages, including C#, C++, and Visual Basic, to name a few.
24+
Code snippets are small blocks of reusable code that you can add to a code file. Snippets usually contain often-used code blocks such as `Try...Finally` or `If...Else` blocks. You can also use code snippets to add entire classes or methods.
25+
26+
A code snippet has a title that you use to select the snippet, and can have a shortcut name you can type to insert the snippet. Snippets can also contain replacement parameters, which act as placeholders. Snippets are available for many languages, including C#, C++, and Visual Basic.
27+
28+
## Expansion snippets and surround-with snippets
29+
30+
You can add two kinds of code snippets to your code:
31+
32+
- An *expansion* snippet is added at a specified insertion point.
33+
- A *surround-with* snippet is added around selected code, and is available only for C# and C++.
2534

2635
## Find available snippets
2736

28-
Before you [create a snippet of your own](walkthrough-creating-a-code-snippet.md), you can check the available snippets for a language, select **Tools** > **Code Snippets Manager** from the menu bar (or, press **Ctrl**+**K**, **Ctrl**+**B**) and then select the language you want from the drop-down menu at the top of the dialog box.
37+
To view the available snippets for a language, select **Tools** > **Code Snippets Manager** from the menu bar, or press **Ctrl**+**K**, **B**, and then select the language you want from the dropdown menu.
2938

3039
![Screenshot of the Code Snippets Manager dialog box.](media/code-snippets-manager.png)
3140

32-
## Add a snippet to your code file
41+
To create and import your own snippet so it appears in the snippet list, see [Walkthrough: Create a code snippet in Visual Studio](walkthrough-creating-a-code-snippet.md).
3342

34-
In Visual Studio, there are several different ways you can add a code snippet to a code file:
43+
## Add a snippet to a code file
3544

36-
- On the menu bar, choose **Edit** > **IntelliSense** > **Insert Snippet**
45+
To add a snippet to a code file:
3746

38-
- From the right-click or context menu in the code editor, choose **Snippet** > **Insert Snippet**
47+
1. In the code editor, place your cursor or select C# or C++ code where you want to add the snippet.
3948

40-
- From the keyboard, press **Ctrl**+**K**,**Ctrl**+**X**
49+
1. Take one of the following actions:
4150

42-
## Expansion snippets and surround-with snippets
51+
- On the Visual Studio menu bar, choose **Edit** > **IntelliSense** > **Insert Snippet** or **Edit** > **IntelliSense** > **Surround With**.
52+
- Right-click, and choose **Snippet** > **Insert Snippet** or **Snippet** > **Surround With** from the context menu.
53+
- From the keyboard, press **Ctrl**+**K**, **X** for an expansion snippet, or **Ctrl**+**K**, **S** for a surround-with snippet.
4354

44-
There are two kinds of code snippets you can add to your code.
55+
1. Double-click the snippet you want in the dropdown list, or type the snippet shortcut and press **Tab**.
4556

46-
|Snippet type | How it's used |
47-
|---------|---------|
48-
|*expansion* | Added at a specified insertion point and might replace a snippet shortcut |
49-
|*surround-with* | Added around a selected block of code (available for C# and C++ only) |
57+
### Add an expansion snippet
5058

51-
In the following example of an expansion snippet in C#, the shortcut `tryf` adds a try-finally block:
59+
For example, the C# expansion snippet `tryf` adds the following `Try...Finally` block:
5260

5361
```csharp
5462
try
@@ -61,17 +69,18 @@ finally
6169
}
6270
```
6371

64-
Here's how to add this expansion snippet:
72+
To add this expansion snippet:
6573

66-
1. In the code window, position your cursor where you want to add the snippet.
74+
1. In the code editor, position your cursor where you want to add the snippet.
75+
1. Right-click and select **Insert Snippet** from the right-click context menu, or use the menu bar or keyboard commands.
76+
1. Open the **Visual C#** folder, and then double-click **tryf** in the dropdown list, or select **tryf** and press **Tab**.
6777

68-
1. Select **Insert Snippet** from the right-click context menu.
78+
>[!NOTE]
79+
>You can also place your cursor at the insertion point, type *tryf*, and then press **Tab** twice.
6980
70-
Alternatively, from the **Edit** menu, select **IntelliSense** and then select the **Surround With** command.
81+
### Add a surround-with snippet
7182

72-
1. Select **Visual C#**, then type `tryf`, and then press **Tab**. (Or, you can type `tryf` and press **Tab** twice.)
73-
74-
In the following example of a surround-with snippet in C++, the shortcut `if` can be used either as an insertion snippet or as a surround-with snippet. If you select a line of code (for example `return FALSE;`), and then choose **Surround With** > **if**, the snippet is expanded around the line:
83+
In the following example of a C++ snippet, the shortcut `if` can be used either as an insertion snippet or a surround-with snippet. If you select the code line `return FALSE;` and then choose **Surround With** > **if**, the snippet is expanded around the line.
7584

7685
```cpp
7786
if (true)
@@ -80,18 +89,11 @@ if (true)
8089
}
8190
```
8291

83-
::: moniker range="<=vs-2019"
84-
> [!TIP]
85-
> In Visual Studio 2019, you can use the **Ctrl**+**K**, **Ctrl**+**S** keyboard shortcut to insert a surround-with code snippet.
86-
::: moniker-end
87-
8892
## Snippet replacement parameters
8993

90-
Snippets can contain replacement parameters, which are placeholders that you must replace to fit the code you're writing. In the previous example, `true` is a replacement parameter, which you'd replace with the appropriate condition. The replacement you make is repeated for every instance of the same replacement parameter in the snippet.
94+
Snippets can contain replacement parameters, which are placeholders you must replace to fit the code you're writing. In the previous example, `true` is a replacement parameter, which you replace with the appropriate condition. The replacement is repeated for every instance of that replacement parameter in the snippet.
9195

92-
For example, in Visual Basic there's a code snippet that inserts a property.
93-
94-
To insert the snippet:
96+
For example, Visual Basic has a code snippet that defines a property. To insert the snippet:
9597

9698
1. In the right-click context menu in a Visual Basic code file, select **Snippet** > **Insert Snippet**.
9799
2. Select **Code Patterns** > **Properties, Procedures, Events** > **Define a Property**.
@@ -112,63 +114,58 @@ Public Property NewProperty() As String
112114
End Property
113115
```
114116

115-
If you change `newPropertyValue` to `m_property`, then every instance of `newPropertyValue` is changed. If you change `String` to `Int` in the property declaration, then the value in the set method is also changed to `Int`.
117+
If you change `newPropertyValue` to `m_property`, every instance of `newPropertyValue` changes. If you change `String` to `Int` in the property declaration, the value in the `Set` method also changes to `Int`.
116118

117119
## Best practices for using code snippets
118120

119121
The code in a code snippet shows only the most basic way to do something. For most applications, the code must be modified to suit the application.
120122

121-
### Handling exceptions
123+
### Exception handling
122124

123-
Typically, code snippet Try...Catch blocks catch and rethrow all exceptions. That may not be the right choice for your project. For each exception, there are several ways to respond. For examples, see [How to: Handle an exception using try/catch (C#)](/dotnet/csharp/programming-guide/exceptions/how-to-handle-an-exception-using-try-catch) and [Try...Catch...Finally statement (Visual Basic)](/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement).
125+
Typically, code snippet `Try...Catch` blocks catch and rethrow all exceptions, but that might not be the right choice for your project. For each exception, there are several ways to respond. For examples, see [How to handle an exception using try/catch (C#)](/dotnet/csharp/programming-guide/exceptions/how-to-handle-an-exception-using-try-catch) and [Try...Catch...Finally statement (Visual Basic)](/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement).
124126

125127
### File locations
126128

127-
When you adapt file locations to your application, you should think about the following:
129+
When you adapt file locations to your application, be sure to consider the following factors.
130+
131+
- Find an accessible location. Users might not have access to the computer's *Program Files* folder, so storing user files with the application files might not work.
128132

129-
- Finding an accessible location. Users may not have access to the *Program Files* folder of the computer, so storing files with the application files may not work.
133+
- Find a secure location. Storing files in the root folder like *C:\\* isn't secure. For application data, it's best to use the *Application Data* folder. For individual user data, the application can create a file in each user's *Documents* folder.
130134

131-
- Finding a secure location. Storing files in the root folder (*C:\\*) is not secure. For application data, we recommend the *Application Data* folder. For individual user data, the application can create a file for each user in the *Documents* folder.
135+
- Use a valid file name. You can use the <xref:System.Windows.Forms.OpenFileDialog> and <xref:System.Windows.Forms.SaveFileDialog> controls to reduce the likelihood of invalid file names.
132136

133-
- Using a valid file name. You can use the <xref:System.Windows.Forms.OpenFileDialog> and <xref:System.Windows.Forms.SaveFileDialog> controls to reduce the likelihood of invalid file names. Be aware that between the time the user selects a file and the time your code manipulates the file, the file may be deleted. In addition, the user may not have permissions to write to the file.
137+
- Be aware that a user-selected file could be deleted before your code can manipulate the file. Also, a user might not have permission to write to the file.
134138

135139
### Security
136140

137-
How secure a snippet is depends on where it is used in the source code and how it is modified once it is in the code. The following list contains a few of the areas that must be considered.
141+
The security of a snippet depends on where you use it in the source code and how you modify it once it's in the code. The following list contains a few areas to consider:
138142

139143
- File and database access
140-
141144
- Code access security
142-
143-
- Protecting resources (such as event logs, registry)
144-
145+
- Protecting resources such as event logs and registry
145146
- Storing secrets
146-
147147
- Verifying inputs
148-
149148
- Passing data to scripting technologies
150149

151-
For more information, see [Securing applications](../ide/securing-applications.md).
150+
For more information, see [Secure applications](securing-applications.md).
152151

153152
### Downloaded code snippets
154153

155-
Code snippets installed by Visual Studio are not in themselves a security hazard. However, they can create security risks in your application. Snippets downloaded from the Internet should be treated like any other downloaded content - with extreme caution.
156-
157-
- Download snippets only from sites you trust, and use up-to-date virus software.
154+
Code snippets installed by Visual Studio aren't security hazards themselves. However, they can create security risks in your application. Snippets downloaded from the internet should be treated with extreme caution, like any other downloaded content. Download snippets only from sites you trust, and use up-to-date virus software.
158155

159-
- Open all downloaded snippet files in Notepad or the XML editor of Visual Studio and review them carefully before installing them. Look for the following issues:
156+
Open all downloaded snippet files in Notepad or the XML editor of Visual Studio and review them carefully before installing them. Beware of the following issues:
160157

161-
- The snippet code could damage your system if you execute it. Read the source code carefully before running it.
158+
- Snippet code that could damage your system if you execute it. Read the source code carefully before running it.
162159

163-
- The Help URL block of the snippet file can contain URLs that execute a malicious script file or display an offensive website.
160+
- `HelpURL` blocks in snippet files containing URLs that execute malicious script files or display offensive websites.
164161

165-
- The snippet may contain references that are added silently to your project and may be loaded from anywhere on your system. These references may have been downloaded to your computer from where you downloaded the snippet. The snippet may then make a call to a method in the reference that executes malicious code. To protect yourself against such an attack, review the Imports and References blocks of the snippet file.
162+
- Snippets containing references that are added silently to your project and can be loaded from anywhere on your system. You might download these references from the same location you download a snippet from. The snippet could then call a method in the reference that executes malicious code. To detect such references, review the `Imports` and `References` blocks of the snippet file.
166163

167164
## Related content
168165

169-
- [Create a code snippet](walkthrough-creating-a-code-snippet.md).
170-
- [Distribute code snippets](how-to-distribute-code-snippets.md)
166+
- [Walkthrough: Create a code snippet in Visual Studio](walkthrough-creating-a-code-snippet.md)
167+
- [Distribute code snippets as a Visual Studio extension](how-to-distribute-code-snippets.md)
171168
- [C# code snippets](visual-csharp-code-snippets.md)
172-
- [C++ code snippets](visual-cpp-code-snippets.md)
169+
- [Visual C++ code snippets](visual-cpp-code-snippets.md)
173170
- [Code snippets schema reference](code-snippets-schema-reference.md)
174171

1.66 KB
Loading

0 commit comments

Comments
 (0)