This documentation is archived and is not being maintained.

Code Snippets (C#) 

Visual Studio provides a new feature called code snippets. You can use code snippets to type a short alias, and then expand it into a common programming construct. 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, then choose a code snippet which will incorporate the selected lines of code. For example, selecting lines of code then activating the for code snippet 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

Code snippets are normally used in the Code Editor by typing a short name for the alias — a code snippet shortcut — then pressing TAB. The IntelliSense menu also offers an Insert Code Snippet menu command, providing a list of available code snippets for insertion into the Code Editor. You can activate the code snippet list by typing CTRL+K, then X. 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 on a field places the cursor in the field, and double-clicking on 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 will cancel field editing and return 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 utilize custom code snippets, in addition to the code snippets that are included with Visual Studio by default. For more information on creating custom code snippets, see Creating Code Snippets.


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

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

See Also