Code Snippet for Automatically Implemented Property

Code snippets allows programmers to extend auto-complete feature of Visual Studio. Code snippets enables a programmer to create an auto-complete that will appear in the intellisense. It also allows a programmer to define the logic of auto complete.

Behind the hood, code snippets are the XML files in a pre-defined format. Visual Studio IDE reads these XML files and made them available in intellisense. To check code snippet in live, type 'if' in c# code file and press tab two times. It inserts the skeleton of if structure automatically. This snippet came pre-installed with Visual Web Developer. But you can create your own snippets.

Visual Web Developer 2008 Express Edition provides two code snippets for properties - prop and propg. Prop inserts code for a regular property and propg inserts code for automatically implemented property with private set accessor. But in most cases we require automatically inserted property with get and set accessors having public access as in the code below:

public int MyProperty { get; set; }

Here is the code snippet for this.

propa.snippet file

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propa</Title>
            <Shortcut>propa</Shortcut>
            <Description>Code snippet for an automatically implemented property with 'get' and 'set' accessors</Description>
            <Author>Yanesh Tyagi</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>
            </Declarations>
            <Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

What Does This XML Mean:

The code snippet XML has two sections - <Header> and <Snippet>. Header contains snippet's meta data like title, shortcut, description etc. The text in the <Shortcut> section appears in the intellisense. The other section <Snippet> contains the actual data that will be inserted. The actual text that will be inserted is defined in the <Code> section. Note that the replaceable sections are sandwiched between '$'. Scott Mitchell describes the code snippets in more details in his article at 4Guys. 

How To Install:

  1. Create an xml file and name it propa.snippet.
  2. Save this file in My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets folder. VWD IDE automatically reads the snippets from this folder.

How To Use

Place the cursor where you want to insert the snippet in c# code file. Type propa and press tab two times. The following code will be inserted at the location of cursor.

public int MyProperty { get; set; }

The following image shows propa snippet in action.

image

Download this xml file ( 1kb zip).

kick it on DotNetKicks.com

2 Comments

Comments have been disabled for this content.