Using Code Snippets in Visual C #

by Web-Host-Site.com 13. April 2010 23:44

More Efficient C# Coding with Code Snippets

Using Code Snippets is a great way to have programmatic logic at your finger tips. Instead of repetitiously typing code, you can use mouse clicks or a few keyboard short cuts to have code inserted into your programs.

You can insert C# Code Snippets using the Code Snippet Picker or by typing the shortcut name of the snippet and pressing Tab. The following is what is listed in MSDN on how to use Code Snippets in C# (C Sharp), it's really easy so you should be in the habit of using them.

How to: Use Code Snippets (C#)

To use code snippets through the Insert Code Snippet Menu

  1. In the Visual Studio integrated development environment (IDE), open the file that you want to edit.
  2. In the Code Editor, put the cursor where you want to insert the code snippet.
  3. Launch the Insert Code Snippet menu in one of three ways:
    • Press CTRL+K, CTRL+X.  -OR-
    • On the Edit menu, point to IntelliSense, and then click Insert Snippet. -OR-
    • Right-click the mouse and then select the Insert Snippet command on the shortcut menu.
  4. Select the code snippet from the code snippet inserter and then press TAB or ENTER or you can type the name of the code snippet, and then press TAB or ENTER.

To use code snippets through the snippet’s shortcut

  1. In the Visual Studio IDE, open the file that you intend to change.
  2. In the Code Editor, put the cursor where you want to insert the code snippet.
  3. Type the shortcut for the code snippet that you want to add to your code.  OR   Press CTRL+SPACE to view the completion list and then select the snippet shortcut from the list.
  4. Press TAB, TAB to invoke the code snippet.

This procedure can only invoke snippets that have non-null Shortcut Element (IntelliSense Code Snippets). To view a snippet's shortcut element in the Code Snippets Manager, click Code Snippets Manager on the Tools menu.

Tags: , ,