What does the following code snippet do

Walkthrough: Creating a Snippet of Code

A snippet of code can be created in just a few steps. All you have to do is create an XML file, fill in the appropriate elements and add the code. You can optionally use substitution parameters and project references. Import the code snippet using the button Import in the Code Snippet Manager (Tools > Code Snippet Manager) into your Visual Studio installation.

Cutout template

The following XML is a simple cutout template:

Create a snippet of code

  1. Create a new XML file in Visual Studio and add the template shown above.

  2. Enter the title of the section in the item title a. Use the title Square root.

  3. Enter the language of the excerpt into the languages-Attribute of codeElements. For C # you are using CSharp, for Visual Basic VB and for C ++ CPP.

  4. Paste the code snippet in the CDATA-Section within the item code added.

    For C #:

    Or for Visual Basic:

    Note

    You can't specify how lines of code in the CDATA- Section of a code snippet should be indented or formatted. When pasting, the voice service automatically formats the pasted code.

  5. Save the code snippet as a SquareRoot.snippet (You can choose any location).

Import a snippet of code

  1. You can extract a code snippet using the Code Snippet Manager import it into your Visual Studio installation. Choose Tools > Code Snippet Manager out.

  2. Click the button Import.

  3. Browse to the location of the code snippet from the previous procedure, click it, and then click to open.

  4. The dialog box Import code snippet opens and prompts you to choose where to add the cutout from the options in the right pane. The option My code snippets should be available for selection. Select this option, click Complete and then on OK.

  5. The code snippet is copied to one of the following locations, depending on the code language:

    % USERPROFILE% \ Documents \ Visual Studio 2017 \ Code Snippets \ Visual C # \ My Code Snippets
    % USERPROFILE% \ Documents \ Visual Studio 2017 \ Code Snippets \ Visual Basic \ My Code Snippets

    % USERPROFILE% \ Documents \ Visual Studio 2019 \ Code Snippets \ Visual C # \ My Code Snippets
    % USERPROFILE% \ Documents \ Visual Studio 2019 \ Code Snippets \ Visual Basic \ My Code Snippets

  6. Test the snippet by opening a C # or Visual Basic project. With a code file open in the editor, select Options from the context menu Code snippets > Insert cutout and then My code snippets out. There should be a snippet of code called Square root are displayed. Double click on it.

    The code snippet is inserted into the code file.

Description and link fields

  1. Description fields provide information about the section of code when viewed in Code Snippet Manager. The link is a tag that users can enter to paste their code snippet. Edit the snippet you added by editing the file % USERPROFILE% \ Documents \ Visual Studio 2017 \ Code Snippets \ [Visual C # or Visual Basic] \ My Code Snippet \ SquareRoot.snippet to open.
  1. Description fields provide information about the section of code when viewed in Code Snippet Manager. The link is a tag that users can enter to paste their code snippet. Edit the snippet you added by editing the file % USERPROFILE% \ Documents \ Visual Studio 2019 \ Code Snippets \ [Visual C # or Visual Basic] \ My Code Snippet \ SquareRoot.snippet to open.

tip

Because you are editing the file in the directory where Visual Studio placed it, you do not need to re-import the file into Visual Studio.

  1. Add to the Header-Element author- and DescriptiveItems and fill them out.

  2. The HeaderElement should look something like this:

  3. Open the Code Snippet Managerand select your code snippet. Notice that the fields description and author in the right area are now filled.

  4. To add a link, add an item shortcut in the element Header add:

  5. Save the cutout file again.

  6. To test the link, open the project you used earlier, type sqrt in the editor and press the TAB KEY (once for Visual Basic, twice for C #).

    The code snippet is inserted.

Replacement parameters

You may want parts of a code snippet to be user-replaceable. For example, a user should be able to replace a variable name with a name from his current project. You can provide two types of substitutions: literals and objects. Use the literal element to indicate a replacement for an element of code that is entirely contained in the snippet but is likely to change after being inserted into the code (such as a string or numeric value). Use the object element to identify an element that is required by the code snippet but is likely to be defined outside of the code snippet itself (e.g. an object instance or a control).

  1. To allow the user to easily replace the number from which to calculate the square root, change the element Snippet of the file SquareRoot.snippet as follows:

    Notice that the literal replacement is given an ID (). This ID is referenced from the code snippet by surrounding it with characters:

  2. Save the cutout file.

  3. Open a project and paste the cutout.

    The code snippet is pasted and the editable literal is highlighted for replacement. Hover over the replacement parameter to display the tooltip for the value.

    tip

    If a code snippet contains more than one replaceable parameter, you can use the TAB KEY Press to navigate between parameters and change values.

Import a namespace

You can use a snippet of code to add a directive (C #) or statement (Visual Basic) by adding the Imports element. For .NET Framework projects, you can also use the References element to add a reference to the project.

The following XML shows a snippet of code that uses the method in the System.IO namespace and hence the ImportsElement defined to import the System.IO namespace.

See also

Is this page helpful?