Code Snippets in Visual Studio

There are a few tasks in programming that are tedious. Instead of working out the logic, time is spent creating properties or repetitively typing code with a syntax that include lots of special characters. Intellisense is great for these sorts of things, but sometimes it’s not quite enough. Code Snippets to the rescue!

If you are a seasoned programmer, you may have developed a specific way that you like to code things. Consistency is key if you are building complex enterprise level applications. Code Snippets to the rescue!

Code Snippets are little templates of code written in XML that you can create and customize. As you begin typing, Intellisense will offer the available snippets. You can hit the tab key to insert that named snippet into your code. Here’s an example:

Snippet Named “eoPropStr” for a C# Property with a String Data Type

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropStr</Title>
			<Shortcut>eoPropStr</Shortcut>
			<Description>Code snippet for a property with a string data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private string str$property$ = string.Empty;

	public string $property$
	{
		get { return str$property$;}
		set { str$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

This template is a simple text file that is saved with a .snippet extension. It shows several things:

  • Title – shows in the Intellisense popover
  • Description – shows in the intellisense popover
  • Shortcut – the value to type when searching for the snippet
  • Author – helpful information when working in a team and sharing snippets
  • SnippetType – Expansion allows the code snippet to be inserted at the cursor

The snippet text itself must be placed in the inner square brackets of this tag “<![CDATA[]]>”. You can see the place holders that are named in between the dollar sign characters. They are defined above. These are the values that you can update after inserting the snippet in your code.

C# Model Class Properties

Intellisense provides “prop” or “fullprop” but I write my properties a little bit different. This snippet has provided a default value for the variable (= string.Empty;), the data type is statically assigned in the template, and the variable name includes a data type prefix (“str”). This allows for a single placeholder to insert the same value in all the references of the variable and the property name (FirstName).

public class Person
    {
        private string strFirstName = string.Empty;

        public string FirstName
        {
            get { return strFirstName; }
            set { strFirstName = value; }
        }
    }

Snippet Named “eoPropNDte” for a C# Property with a DateTime? Data Type

This is a snippet I use all the time because DateType variables cannot be null. This becomes a problem when a user leaves a textbox blank on a form and the data is put into a model. The property value for a DateTime will assume the default value of “1/1/1900” and when you save that into your database you have now stored a value that the user never intended. By using this nullable DateTime property, you can take a blank value from the user and store it in the database as a null.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropNDte</Title>
			<Shortcut>eoPropNDte</Shortcut>
			<Description>Code snippet for a property with a DateTime? data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private DateTime? dte$property$ = null;

	public DateTime? $property$
	{
		get { return dte$property$;}
		set { dte$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>
    private DateTime? dteBirthday = null;

    public DateTime? Birthday
    {
        get { return dteBirthday; }
        set { dteBirthday = value; }
    }

Add your snippets to Visual Studio

When you have your snippets ready to go, import them into Visual Studio’s Code Snippet Manager.

TIP

I would name your snippets (or at least the shortcut) in a somewhat hierarchical fashion. This allows Intellisense to group your snippets together when presenting the list of options to you.

I have prefixed all my snippets with my initials. Then I specified that they are all properties with “Prop”. Finally, I specified the data type for each type of property.

Resources

Microsoft’s Code Snippet References

C# Snippets

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropStr</Title>
			<Shortcut>eoPropStr</Shortcut>
			<Description>Code snippet for a property with a string data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private string str$property$ = string.Empty;

	public string $property$
	{
		get { return str$property$;}
		set { str$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropNGuid</Title>
			<Shortcut>eoPropNGuid</Shortcut>
			<Description>Code snippet for a property with a Guid? data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private Guid? guid$property$ = null;

	public Guid? $property$
	{
		get { return guid$property$;}
		set { guid$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>



<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropInt</Title>
			<Shortcut>eoPropInt</Shortcut>
			<Description>Code snippet for a property with a int data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private int int$property$ = 0;

	public int $property$
	{
		get { return int$property$;}
		set { int$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>



<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropGuid</Title>
			<Shortcut>eoPropGuid</Shortcut>
			<Description>Code snippet for a property with a Guid data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private Guid guid$property$ = Guid.Empty;

	public Guid $property$
	{
		get { return guid$property$;}
		set { guid$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>



<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropDte</Title>
			<Shortcut>eoPropDte</Shortcut>
			<Description>Code snippet for a property with a DateTime data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private DateTime dte$property$;

	public DateTime $property$
	{
		get { return dte$property$;}
		set { dte$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>



<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropDbl</Title>
			<Shortcut>eoPropDbl</Shortcut>
			<Description>Code snippet for a property with a int data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private double dbl$property$ = 0;

	public double $property$
	{
		get { return dbl$property$;}
		set { dbl$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>



<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropBln</Title>
			<Shortcut>eoPropBln</Shortcut>
			<Description>Code snippet for a property with a Boolean data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private Boolean bln$property$ = false;

	public Boolean $property$
	{
		get { return bln$property$;}
		set { bln$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>



<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>eoPropNDte</Title>
			<Shortcut>eoPropNDte</Shortcut>
			<Description>Code snippet for a property with a DateTime? data type</Description>
			<Author>Erik Ohler</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>property</ID>
					<ToolTip>Property name</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp">
    <![CDATA[private DateTime? dte$property$ = null;

	public DateTime? $property$
	{
		get { return dte$property$;}
		set { dte$property$ = value;}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

JavaScript Snippets

<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>eoTextbox</Title>
    <Author>Erik Ohler</Author>
    <Shortcut>eoTextbox</Shortcut>
    <Description>Code snippet for a jQuery textbox reference</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>controlID</ID>
        <ToolTip>The ID of the textbox control</ToolTip>
        <Default>txt</Default>
      </Literal>
    </Declarations>
    <Code Language="JavaScript"><![CDATA[$$("[id*='$controlID$']").val();]]></Code>
  </Snippet>
</CodeSnippet>


<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>eoLabel</Title>
    <Author>Erik Ohler</Author>
    <Shortcut>eoLabel</Shortcut>
    <Description>Code snippet for a jQuery label reference</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>controlID</ID>
        <ToolTip>The ID of the label control</ToolTip>
        <Default>lbl</Default>
      </Literal>
    </Declarations>
    <Code Language="JavaScript"><![CDATA[$$("[id*='$controlID$']").html();]]></Code>
  </Snippet>
</CodeSnippet>


<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>eoEach</Title>
    <Author>Erik Ohler</Author>
    <Shortcut>eoEach</Shortcut>
    <Description>Code snippet for a jQuery each loop</Description>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>selector</ID>
        <ToolTip>The selector that determines the items to loop through</ToolTip>
        <Default>[id*='']</Default>
      </Literal>
    </Declarations>
    <Code Language="JavaScript">
      <![CDATA[$$("$selector$").each(function (index, item) {

        });]]></Code>
  </Snippet>
</CodeSnippet>

Leave a comment

Design a site like this with WordPress.com
Get started