Udostępnij za pośrednictwem


Code Snippets (C#)

Visual Studio provides a feature called code snippets. Code snippets are ready-made snippets of code you can quickly insert into your code. For example, the for code snippet creates an empty for loop. Some code snippets are surround-with code snippets, which enable you to select lines of code, and then choose a code snippet which incorporates the selected lines of code. For example, when you select lines of code and then activate the for code snippet, it creates a for loop with those lines of code inside the loop block. Code snippets can make writing program code quicker, easier, and more reliable.

Using Code Snippets

To use a code snippet, type its Shortcut Element (IntelliSense Code Snippets) and then press TAB two times. Snippets that have shortcut names appear in the Completion Lists in C#. Tap the UP and DOWN arrow keys in the Completion List to browse all available snippet shortcuts.

Snippets that do not have shortcut values must be used a different way. Press CTRL+K, X in the Code Editor to launch the Insert Snippet menu. Then, select a snippet from the menu and press TAB or ENTER. For more information, see How to: Use Code Snippets (C#) and How to: Use Surround-with Code Snippets.

Once a code snippet has been chosen, the text of the code snippet is inserted automatically at the cursor position. At this point, any editable fields in the code snippet are highlighted in yellow, and the first editable field is selected automatically. The currently selected field is boxed in red. For example, in the for code snippet, the editable fields are the initializer variable (i by default) and the length expression (length by default).

When a field is selected, users can type a new value for the field. Pressing TAB cycles through the editable fields of the code snippet; pressing SHIFT+TAB cycles through them in reverse order. Clicking a field places the cursor in the field, and double-clicking a field selects it. When a field is highlighted, a tooltip might be displayed, offering a description of the field.

Only the first instance of a given field is editable; when that field is highlighted, the other instances of that field are outlined. When you change the value of an editable field, that field is changed everywhere it is used in the code snippet.

Pressing ENTER or ESC cancels field editing and returns the Code Editor to normal.

The default colors for editable code snippet fields can be changed by modifying the Code Snippet Field setting in the Fonts and Colors pane of the Options dialog box. For more information, see How to: Change the Font Face, Size, and Colors Used in the Editor.

Creating Code Snippets

You can create and use custom code snippets, in addition to the code snippets that are included with Visual Studio by default. For more information about how to create custom code snippets, see Creating Code Snippets.

Note

For C# code snippets, the characters that are valid for specifying the <Shortcut> field are as follows: alphanumeric characters, the number sign (#), the tilde character (~), the underscore character (_), and the en dash character (-).

For more information about code snippets that are included in Visual C# by default, see Default Code Snippets.

See Also

Reference

Code Snippet Picker