Fixing ‘prop’ Snippet Compatibility

As you might have gathered from reading my recent post on changing generated code in VS2008, I wasn’t thrilled when Microsoft changed the behavior of the prop snippet between VS2005 and VS2008. It’s almost two years later and my fingers still want to type 'prop’ even when I know that using automatic properties is very, very unlikely to be the thing that I want to generate.

My solution to this consternation was to create a custom snippet. The XML found below can be used to create such a snippet. In this case, the snippet is named ‘propfull’ and its template creates a property complete with the backing variable. It can be loaded into Visual Studio by using the Import button found in the Tools | Code Snippet Manager dialog box. The other possible approach would be to edit the snippet that comes with VS2008, but I’m loath to make that type of change to the default. Changing behavior that others have come to expect would evil, wouldn’t it Microsoft?

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="
http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propfull</Title>
            <Shortcut>propfull</Shortcut>
            <Description>Code snippet for property implemented using a backing store</Description>
            <Author>Bruce Johnson, ObjectSharp Consulting</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>backing</ID>
                    <ToolTip>Backing variable name</ToolTip>
                    <Default>myProperty</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[
private $type$ $backing$;           
public $type$ $property$
{
   get { return $backing$; }
   set { $backing$ = value; }
}$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>