You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
5
5
ms.topic: conceptual
6
6
f1_keywords:
7
7
- vs.ExpansionManagerImport
@@ -19,36 +19,44 @@ dev_langs:
19
19
- CPP
20
20
---
21
21
22
-
# Code snippets: what they are and how to add one
22
+
# Code snippetsand how to use them
23
23
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++.
25
34
26
35
## Find available snippets
27
36
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.
29
38
30
39

31
40
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).
33
42
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
35
44
36
-
- On the menu bar, choose **Edit** > **IntelliSense** > **Insert Snippet**
45
+
To add a snippet to a code file:
37
46
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.
39
48
40
-
- From the keyboard, press **Ctrl**+**K**,**Ctrl**+**X**
49
+
1. Take one of the following actions:
41
50
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.
43
54
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**.
45
56
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
50
58
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:
52
60
53
61
```csharp
54
62
try
@@ -61,17 +69,18 @@ finally
61
69
}
62
70
```
63
71
64
-
Here's how to add this expansion snippet:
72
+
To add this expansion snippet:
65
73
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**.
67
77
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.
69
80
70
-
Alternatively, from the **Edit** menu, select **IntelliSense** and then select the **Surround With** command.
81
+
### Add a surround-with snippet
71
82
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.
75
84
76
85
```cpp
77
86
if (true)
@@ -80,18 +89,11 @@ if (true)
80
89
}
81
90
```
82
91
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
-
88
92
## Snippet replacement parameters
89
93
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.
91
95
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:
95
97
96
98
1. In the right-click context menu in a Visual Basic code file, select **Snippet** > **Insert Snippet**.
@@ -112,63 +114,58 @@ Public Property NewProperty() As String
112
114
EndProperty
113
115
```
114
116
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`.
116
118
117
119
## Best practices for using code snippets
118
120
119
121
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.
120
122
121
-
### Handling exceptions
123
+
### Exception handling
122
124
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).
124
126
125
127
### File locations
126
128
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.
128
132
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.
130
134
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.
132
136
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.
134
138
135
139
### Security
136
140
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:
138
142
139
143
- File and database access
140
-
141
144
- Code access security
142
-
143
-
- Protecting resources (such as event logs, registry)
144
-
145
+
- Protecting resources such as event logs and registry
145
146
- Storing secrets
146
-
147
147
- Verifying inputs
148
-
149
148
- Passing data to scripting technologies
150
149
151
-
For more information, see [Securing applications](../ide/securing-applications.md).
150
+
For more information, see [Secure applications](securing-applications.md).
152
151
153
152
### Downloaded code snippets
154
153
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.
158
155
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:
160
157
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.
162
159
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.
164
161
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.
166
163
167
164
## Related content
168
165
169
-
-[Create a code snippet](walkthrough-creating-a-code-snippet.md).
0 commit comments