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
Copy file name to clipboardExpand all lines: docs/ide/best-practices-for-using-code-snippets.md
+56-53
Original file line number
Diff line number
Diff line change
@@ -4,67 +4,70 @@ ms.custom: ""
4
4
ms.date: "11/04/2016"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
-
ms.technology:
8
-
- "vs-ide-general"
7
+
ms.technology: vs-ide-general
9
8
ms.tgt_pltfrm: ""
10
9
ms.topic: "article"
11
10
helpviewer_keywords:
12
11
- "code snippets, best practices"
13
12
- "code snippets, security"
14
-
ms.assetid: a293ec17-4dd7-4a99-8eeb-99f44a822a8b
15
-
caps.latest.revision: 22
16
13
author: "gewarren"
17
14
ms.author: "gewarren"
18
15
manager: ghogen
19
16
ms.workload:
20
17
- "multiple"
21
18
---
22
-
# Best Practices for Using Code Snippets
23
-
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.
24
-
25
-
## Handling Exceptions
26
-
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# Programming Guide)](/dotnet/csharp/programming-guide/exceptions/how-to-handle-an-exception-using-try-catch) and [Try...Catch...Finally Statement](/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement).
27
-
28
-
## File Locations
29
-
When you adapt file locations to your application, you should think about the following:
30
-
31
-
- 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.
32
-
33
-
- 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 \My Documents folder.
34
-
35
-
- 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.
36
-
37
-
## Security
38
-
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.
39
-
40
-
- File and database access
41
-
42
-
- Code access security
43
-
44
-
- Protecting resources (such as event logs, registry)
45
-
46
-
- Storing secrets
47
-
48
-
- Verifying inputs
49
-
50
-
- Passing data to scripting technologies
51
-
52
-
For more information, see [Securing Applications](../ide/securing-applications.md).
53
-
54
-
## Downloaded Code Snippets
55
-
IntelliSense 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.
56
-
57
-
- Download snippets only from sites you trust, and use up-to-date virus software.
58
-
59
-
- 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:
60
-
61
-
- The snippet code could damage your system if you execute it. Read the source code carefully before running it.
62
-
63
-
- The Help URL block of the snippet file can contain URLs that execute a malicious script file or display an offensive Web site.
64
-
65
-
- 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.
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.
22
+
23
+
## Handling exceptions
24
+
25
+
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).
26
+
27
+
## File locations
28
+
29
+
When you adapt file locations to your application, you should think about the following:
30
+
31
+
- 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.
32
+
33
+
- 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.
34
+
35
+
- 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.
36
+
37
+
## Security
38
+
39
+
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.
40
+
41
+
- File and database access
42
+
43
+
- Code access security
44
+
45
+
- Protecting resources (such as event logs, registry)
46
+
47
+
- Storing secrets
48
+
49
+
- Verifying inputs
50
+
51
+
- Passing data to scripting technologies
52
+
53
+
For more information, see [Securing Applications](../ide/securing-applications.md).
54
+
55
+
## Downloaded code snippets
56
+
57
+
IntelliSense 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.
58
+
59
+
- Download snippets only from sites you trust, and use up-to-date virus software.
60
+
61
+
- 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:
62
+
63
+
- The snippet code could damage your system if you execute it. Read the source code carefully before running it.
64
+
65
+
- The Help URL block of the snippet file can contain URLs that execute a malicious script file or display an offensive Web site.
66
+
67
+
- 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.
Copy file name to clipboardExpand all lines: docs/ide/code-snippets.md
+22-17
Original file line number
Diff line number
Diff line change
@@ -5,29 +5,40 @@ ms.date: "11/04/2016"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology: vs-ide-general
8
-
ms.tgt_pltfrm: ""
9
8
ms.topic: "article"
10
9
f1_keywords:
11
10
- "vs.ExpansionManagerImport"
12
11
- "vs.codesnippetmanager"
13
-
helpviewer_keywords:
14
-
- "code snippets, replacement parameters"
15
-
- "code snippets, surround with"
16
-
- "replacement parameters"
17
-
- "code snippets, expansion"
12
+
helpviewer_keywords:
18
13
- "surround with"
19
14
- "code snippets"
20
15
author: "gewarren"
21
16
ms.author: "gewarren"
22
17
manager: ghogen
18
+
dev_langs:
19
+
- CSharp
20
+
- VB
21
+
- CPP
23
22
ms.workload:
24
23
- "multiple"
25
24
---
26
25
# Code snippets
27
26
28
-
Code snippets are small blocks of reusable code that can be inserted in a code file using a context menu command or a combination of hotkeys. They typically contain commonly-used code blocks such as try-finally or if-else blocks, but they can be used to insert entire classes or methods.
27
+
Code snippets are small blocks of reusable code that can be inserted in a code file using a context menu command or a combination of hotkeys. They typically contain commonly-used code blocks such as `try-finally` or `if-else` blocks, but they can be used to insert entire classes or methods.
29
28
30
-
## Expansion Snippets and Surround-With Snippets
29
+
Code snippets are available for a multitude of programming languages, including C#, C++, Visual Basic, XML, and T-SQL, to name a few. To view all the available installed snippets for a language, open the **Code Snippets Manager** from the **Tools** menu in Visual Studio, and choose the language from the drop-down menu at the top.
Code snippets can be accessed in the following general ways:
34
+
35
+
- On the menu bar, choose **Edit** > **IntelliSense** > **Insert Snippet...**
36
+
37
+
- From the right-click or context menu in the code editor, choose **Snippet** > **Insert Snippet...**
38
+
39
+
- From the keyboard, press **Ctrl**+**K**+**X**
40
+
41
+
## Expansion snippets and surround-with snippets
31
42
32
43
In Visual Studio there are two kinds of code snippet: expansion snippets, which are added at a specified insertion point and may replace a snippet shortcut, and surround-with snippets (C# and C++ only), which are added around a selected block of code.
33
44
@@ -44,7 +55,7 @@ finally
44
55
}
45
56
```
46
57
47
-
You can insert this snippet by clicking **Insert Snippet** in the context menu of the code window, then **Visual C#**, then type `tryf`, and then press **Tab**, or you can type `tryf` and press **Tab** twice.
58
+
You can insert this snippet by clicking **Insert Snippet** in the context menu of the code window, then **Visual C#**, then type `tryf`, and then press **Tab**. Or, you can type `tryf` and press **Tab** twice.
48
59
49
60
An 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 click **Surround With**, then **if**, the snippet is expanded around the line:
50
61
@@ -55,11 +66,11 @@ if (true)
55
66
}
56
67
```
57
68
58
-
## Snippet Replacement Parameters
69
+
## Snippet replacement parameters
59
70
60
71
Snippets can contain replacement parameters, which are placeholders that you must replace to fit the precise code you are writing. In the previous example `true` is a replacement parameter, which you would replace with the appropriate condition. The replacement you make is repeated for every instance of the same replacement parameter in the snippet.
61
72
62
-
For example, in Visual Basic there is a code snippet that inserts a property. Click **Insert Snippet** on the context menu of the code window, then **Code Patterns**, then **Properties, Procedures, Events**, then Define a property. The following code is inserted:
73
+
For example, in Visual Basic there is a code snippet that inserts a property. Click **Insert Snippet** on the context menu of the code window, then **Code Patterns**, then **Properties, Procedures, Events**, then **Define a Property**. The following code is inserted:
63
74
64
75
```vb
65
76
PrivatenewPropertyValueAsString
@@ -75,12 +86,6 @@ End Property
75
86
76
87
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`.
77
88
78
-
## Code Snippet Manager
79
-
80
-
You can see all the code snippets that are currently installed, and their locations on disk, by choosing **Tools**, **Code Snippets Manager**. Snippets are displayed by language.
81
-
82
-
You can add and remove snippet directories with the **Add** and **Remove** buttons in the **Code Snippets Manager** dialog. To add individual code snippets, use the **Import** button.
83
-
84
89
## See also
85
90
86
91
[Walkthrough: Creating a Code Snippet](../ide/walkthrough-creating-a-code-snippet.md)
title: "How to: Use Surround-with Code Snippets | Microsoft Docs"
2
+
title: "How to Use Surroundwith Code Snippets | Microsoft Docs"
3
3
ms.custom: ""
4
4
ms.date: "11/04/2016"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
-
ms.technology:
8
-
- "vs-ide-general"
7
+
ms.technology: vs-ide-general
9
8
ms.tgt_pltfrm: ""
10
9
ms.topic: "article"
11
10
helpviewer_keywords:
12
-
- "Surround-with snippets"
13
-
- "snippets [C#], using Surround With"
14
-
ms.assetid: 23e5288e-7897-417a-9ded-cf60dfc889ee
15
-
caps.latest.revision: 18
11
+
- "surround-with snippets"
12
+
- "snippets [C#], surround with"
16
13
author: "gewarren"
17
14
ms.author: "gewarren"
18
15
manager: ghogen
19
16
ms.workload:
20
17
- "multiple"
21
18
---
22
-
# How to: Use Surround-with Code Snippets
23
-
The following procedures describe how to use surround-with code snippets. Surround-with code snippets are available three ways: through a keyboard shortcut, through the Edit menu, and through the context menu.
24
-
25
-
### To use surround-with code snippets through keyboard shortcut
26
-
27
-
1. In the Visual Studio IDE, open the file that you intend to edit.
28
-
29
-
2. In the Code Editor, select text to surround.
30
-
31
-
3. Type CTRL+K, CTRL+S.
32
-
33
-
4. Select the code snippet from the code snippet list using the mouse, or by typing the name of the code snippet and pressing TAB or ENTER.
34
-
35
-
### To use surround-with code snippets through the Edit menu
36
-
37
-
1. In the Visual Studio IDE, open the file that you intend to edit.
38
-
39
-
2. In the Code Editor, select text to surround.
40
-
41
-
3. From the **Edit** menu, select **IntelliSense** and then select the **Surround With** command.
42
-
43
-
4. Select the code snippet from the code snippet inserter and then press TAB or ENTER.
44
-
45
-
Alternatively, you can type the name of the code snippet, and then press TAB or ENTER.
46
-
47
-
### To use surround-with code snippets through the context menu
48
-
49
-
1. In the Visual Studio IDE, open the file that you intend to edit.
50
-
51
-
2. In the Code Editor, select text to surround.
52
-
53
-
3. Right-click the selected text and then select the **Surround With** command from the context menu.
54
-
55
-
4. Select the code snippet from the code snippet inserter and then press TAB or ENTER.
56
-
57
-
Alternatively, you can type the name of the code snippet, and then press TAB or ENTER.
The following procedures describe how to use surround-with code snippets. Surround-with code snippets are available three ways: through a keyboard shortcut, through the **Edit** menu, and through the context menu.
22
+
23
+
## To use surround-with code snippets through keyboard shortcut
24
+
25
+
1. In the Visual Studio IDE, open the file that you intend to edit.
26
+
27
+
1. In the Code Editor, select text to surround.
28
+
29
+
1. Type **Ctrl**+**K**, **Ctrl**+**S**.
30
+
31
+
1. Select the code snippet from the code snippet list using the mouse, or by typing the name of the code snippet and pressing **Tab** or **Enter**.
32
+
33
+
## To use surround-with code snippets through the Edit menu
34
+
35
+
1. In the Visual Studio IDE, open the file that you intend to edit.
36
+
37
+
1. In the Code Editor, select text to surround.
38
+
39
+
1. From the **Edit** menu, select **IntelliSense** and then select the **Surround With** command.
40
+
41
+
1. Select the code snippet from the code snippet inserter and then press **Tab** or **Enter**.
42
+
43
+
Alternatively, you can type the name of the code snippet, and then press **Tab** or **Enter**.
44
+
45
+
## To use surround-with code snippets through the context menu
46
+
47
+
1. In the Visual Studio IDE, open the file that you intend to edit.
48
+
49
+
1. In the Code Editor, select text to surround.
50
+
51
+
1. Right-click the selected text and then select the **Surround With** command from the context menu.
52
+
53
+
1. Select the code snippet from the code snippet inserter and then press **Tab** or **Enter**.
54
+
55
+
Alternatively, you can type the name of the code snippet, and then press **Tab** or **Enter**.
0 commit comments