Visual Studio Code Snippets

I never really used the new Code Snippet feature in Visual Studio 2005.  Since I've been spending time with Castle lately I put together a few snippets after feeling a strong pattern in my fingers.  These should be good in general for any DI framework.  Enjoy.  (If you are viewing through the web, sorry about the dark colors.  I recently switched to a darker color scheme in an attempt to reduce eye strain.)

Check if a a passed in argument is null and throw an exception accordingly:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>argn</Title>
      <Shortcut>argn</Shortcut>
      <Description>Perform an argument null check</Description>
      <Author>Bill Pierce</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>argument</ID>
          <ToolTip>Argument name</ToolTip>
          <Default>Arg</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[if ($argument$ == null) throw new ArgumentNullException("$argument$");]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Declare a string field/property that defaults to string.Empty and converts nulls to string.Empty:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>props</Title>
      <Shortcut>props</Shortcut>
      <Description>Code snippet for a string property and backing field</Description>
      <Author>Bill Pierce</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <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 string $field$ = string.Empty;
 
  public virtual string $property$
  {
    get { return $field$;}
    set 
    { 
      if( value == null ) value = string.Empty;
      $field$ = value;
    }
  }
  $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Declare a propery that is set through dependency injection:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propdi</Title>
      <Shortcut>propdi</Shortcut>
      <Description>Code snippet for property set through dependency injection</Description>
      <Author>Bill Pierce</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 virtual $type$ $property$
  {
    protected get 
    { 
      if ($field$ == null) throw new ArgumentNullException("$property$");
      return $field$;
    }
    set 
    { 
      if (value == null) throw new ArgumentNullException("$property$");
      $field$ = value;
    }
  }
  $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

posted @ Wednesday, October 04, 2006 2:45 PM


Print

Comments on this entry:

# re: Visual Studio Code Snippets

Left by Willie Tilton at 10/4/2006 4:23 PM
Gravatar

Do the darker colors help? I thought about having the high contrast on, but never got around to testing it. The strain on my eyes is getting worse as I get older. I'm old...

Your comment:



 (will not be displayed)


 
 
 
Please add 8 and 7 and type the answer here:
 

Live Comment Preview:

 
«August»
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456