In this section, I will demonstrate on a ready-made code snippet that ships with Visual Studio 2005.
Listing 1 below shows the property code snippet that ships with Visual Studio 2005.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>prop</Title>
<Shortcut>prop</Shortcut>
<Description>Code snippet for property and backing field</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[private $type$ $field$;
public $type$ $property$
{
get { return $field$;}
set { $field$ = value;}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
In the Header section, you can see the informative information about the code snippet including a title, a shortcut, a description, an author, and snippet types.
The Snippet section includes two sections, the Declarations and the Code section.
As mentioned above, the Declarations section contains definitions for the replicable values inside the code snippet. In this example, the prop code snippet should define three replicable values, the private field name, the property name, and finally the property data type.
The mandatory code section includes the usual code for a property. You can see the replicable values surrounded by the $ sign. Those values have been defined above in the Declarations section. It is the developer using the code snippet who would replace those values with proper values.