Skip Navigation Links  
Skip Navigation Links
Home
ADO.NET
ASP.NET
C#Expand C#
Visual Basic
 

the .NET professional resource

  C# code snippets

 
 Site information offered by Developers MVP (formers and/or actives)

How to create a code snippet in C#

Using an existing .snippet file

Author: Guillermo 'guille' Som
Published: Sat, 01 Apr 2006
Revised: Sat, 01 Apr 2006
   

This article explains how to create a new code snippet using an existing .snippets file

 

Steps to create a C# Code Snippet

  1. Open in a text editor a file with the .snippet extension, (in listing 1 you can see a sample snippet code).
  2. Add the title to show and the Description in the propper element.
  3. Add the Shortcut and the snippet type.
  4. In the Snippet tree, add the namespaces to import.
  5. Next, add the Declarations (variables name and type), a tooltip to show and the default value, all these settings in a Literal element, one per variable.
  6. The last thing to do is to add the code to show when the snippet is inserted in the code file. In the Code element, add a Language and a Kind attributes.
  7. Next put inside a <![CDATA[ ... ]]> tag the code to show.
  8. And that's all!!!

If we use the snippet shown in the source code listing 1, we'll see it as shown in figure 1.

 

Figure 1. The sample snippet in action.

 

<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http:"//schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Random Number</Title>
      <Author>Microsoft Corporation</Author>
      <Description>Generates a random integer between an upper bound and a lower bound.</Description>
      <Shortcut>mathrandom</Shortcut>
      <SnippetTypes>
          <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Imports>
        <Import>
          <Namespace>System</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal>
          <ID>LowerBound</ID>
          <Type>int</Type>
          <ToolTip>Replace with the smallest integer you want in the result set.</ToolTip>
          <Default>1</Default>
        </Literal>
        <Literal>
          <ID>UpperBound</ID>
          <Type>int</Type>
          <ToolTip>Replace with one more than the largest number you want in the result set.</ToolTip>
          <Default>6</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="method body"><![CDATA[Random generator = new Random();
int randomValue;
// Generates numbers between 1 and 5, inclusive.
randomValue = generator.Next($LowerBound$, $UpperBound$);
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Listing 1. A sample code snippet to generate a random integer between tow numbers.

 

Related links:

How to install a C# code snippet
 
 
Link a la versión en español de este artículo en elGuille.info Link to the Spanish version of this article at elGuille.info
8 points from 2 users (views: 112)
To rate this article, please click a rating, and then click the Rank this article button
dotNetPro.info v0.0.0.79 - ASP.NET 2.0 (v2.0.50727.832) - Last updated: jue, 19 jul 2007 07:03:55 GMT
  You can use the code for free, if you want to publish it in a Web site, please contact: info@dotNetPro.info