Introduction
Visual Studio allows you to insert some commonly used blocks of code by typing the appropriate keyword and pressing tab. This is called code snippets. If you want to learn more about code snippets and how to create them, have a look at Abhishek Sur's article, Microsoft's FAQ or Microsoft's HowTos on the subject.
Here, I list some code snippets that I find useful.
How to Install
Copy the snippet into the following folder (assuming you use VS2010, it may be different for other versions):
C:\Users\{your username}\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets
The file you save should have a .snippet
extension.
How to Use
Normal Way
In Visual Studio, do the following:
- In your code, type the shortcut of the snippet you want to use. If you do not want to type out the full name, type the first few letters and then press
Ctrl + Space
to let Intellisense give you a list of available variables, methods, snippets, etc. to choose from. Select the snippet you want to use. - Press
tab
to expand the snippet. This will give you a piece of code with some sections highlighted in yellow. You can cycle through these with the tab
key. If you overwite these sections with your own text, that text will also replace the relevant other text. For instance, in the example below, notice that the int
in the private
variable is selected, and that the int
in the public
property has a box around it. If you change the private int
to a double
, the public
property will also change to a double
as soon as you tab
away from the int
.
- When you are done changing the highlighted areas as you see fit, press
enter
to finish the code snippet. After this, the code in the snippet will behave just like regular code, i.e., if you want to change the variable and property back to int
, you will have to change both. Finishing the code snippet in this way can also have other effects. For instance, the builtin switch
snippet gives you a list of all possible values when you switch on an enum
. In other words,
gives you:
when you press enter
.
Alternative Way
If you right click on your code, you will see the "Insert Snippet" and "Surround With" options. These will give you an Intellisense popup with a hierarchical list of available code snippets. If you have some code selected when you right click and you select "Surround With", the code snippet will be placed around the selected code (for instance, the if
snippet places an if
statement, its condition and its opening brace before the selected code, and a closing brace after the selected code).
The Code Snippets
Get Current Method Name
Use reflection to get the names of the current namespace, class and method, separated by periods. This is useful for logging purposes.
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<codesnippet format="1.0.0">
<Header>
<Title>methodName</Title>
<shortcut>methodName</shortcut>
<description>Code snippet for inserting code
to return the current method name dynamically</description>
<author>BotCar</author>
<snippettypes>
<snippettype>Expansion</snippettype>
</snippettypes>
</Header>
<snippet>
<![CDATA[MethodInfo.GetCurrentMethod().DeclaringType +
"." + MethodInfo.GetCurrentMethod().Name$end$]]>
Property with OnPropertyChanged
Exactly like Microsoft's propfull
snippet, except this one also has a call to OnPropertyChanged
. This is intended to be used in an MVVM ViewModel
. Notice that the name passed to OnProprtyChanged
is obtained with reflection. This is more expensive than using a string
literal, but is more refactor-friendly. If you prefer to use a string
literal instead, change the OnPropertyChanged
call in the snippet to OnPropertyChanged("$property$");
. Also note that the SubString
cuts off the first four characters of the method name, because the method name is the property name with a get_
or set_
prefix.
Nicolas Dorier pointed out that you don't need reflection to get the name of the property. You can use CallerMemberNameAttribute instead. This is only available from .NET 4.5 onwards, though.
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<codesnippet format="1.0.0">
<Header>
<Title>vmprop</Title>
<shortcut>vmprop</shortcut>
<description>Code snippet for property and backing field,
as well as OnPropertyChange</description>
<author>BotCar</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>
<![CDATA[private $type$ $field$;
public $type$ $property$
{
get { return $field$;}
set
{
$field$ = value;
OnPropertyChanged(MethodInfo.GetCurrentMethod().Name.Substring(4));
}
}
$end$]]>
Invoke Method on the GUI Thread
Invoke a method on the GUI thread (well, technically the thread that created the control, but that's typically the GUI thread). this
is the default control on which to invoke, because this snippet will likely be used within a Form
's class.
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<codesnippet format="1.0.0">
<Header>
<Title>invokeGUI</Title>
<shortcut>invokeGUI</shortcut>
<description>Code snippet for Invoke
</description>
<author>BotCar</author>
<snippettypes>
<snippettype>Expansion</snippettype>
</snippettypes>
</Header>
<snippet>
<declarations>
<literal>
<id>control</id>
<tooltip>Control on which to invoke
</tooltip>
<default>this</default>
</literal>
<literal>
<id>delegate</id>
<tooltip>Delegate to invoke</tooltip>
<default>DelegateToInvoke</default>
</literal>
<literal>
<id>method</id>
<tooltip>Method to invoke</tooltip>
<default>MethodToInvoke</default>
</literal>
<literal>
<id>params</id>
<tooltip>Parameters to pass to the method
</tooltip>
<default>Params</default>
</literal>
</declarations>
<![CDATA[$control$.Invoke(new $delegate$($method$), new object[] { $params$ });
$end$]]>
Invoke Method on a New Thread
Kick off a method on a new thread.
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<codesnippet format="1.0.0">
<Header>
<Title>beginInvoke</Title>
<shortcut>beginInvoke</shortcut>
<description>Code snippet for BeginInvoke
</description>
<author>BotCar</author>
<snippettypes>
<snippettype>Expansion</snippettype>
</snippettypes>
</Header>
<snippet>
<declarations>
<literal>
<id>delegate</id>
<tooltip>Delegate to invoke</tooltip>
<default>DelegateToInvoke</default>
</literal>
<literal>
<id>method</id>
<tooltip>Method to invoke</tooltip>
<default>MethodToInvoke</default>
</literal>
<literal>
<id>params</id>
<tooltip>Parameters</tooltip>
<default>params, </default>
</literal>
<literal>
<id>callback</id>
<tooltip>Callback</tooltip>
<default>null</default>
</literal>
<literal>
<id>object</id>
<tooltip>Object</tooltip>
<default>null</default>
</literal>
</declarations>
<![CDATA[new $delegate$($method$).BeginInvoke($params$$callback$, $object$);
$end$]]>
#region with Name at Both Ends
Identical to Microsoft's #region
snippet, except that it has a tag to identify the region at both ends. This is useful when tidying up existing code that has huge blocks of code that you want to place in regions. The name at the end makes it easier to identify the region without scrolling up to look at the region's name or the code. Note that the text after #endregion
doesn't actually do anything, it is purely cosmetic.
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<codesnippet format="1.0.0">
<Header>
<Title>#region</Title>
<shortcut>region</shortcut>
<description>Code snippet for #region with region name at both ends.
</description>
<author>BotCar</author>
<snippettypes>
<snippettype>Expansion</snippettype>
<snippettype>SurroundsWith</snippettype>
</snippettypes>
</Header>
<snippet>
<declarations>
<literal>
<id>name</id>
<tooltip>Region name</tooltip>
<default>MyRegion</default>
</literal>
</declarations>
<![CDATA[#region $name$
$selected$ $end$
#endregion $name$]]>
If...else block
Microsoft provides an if
snippet and an else
snippet, so why not an if...else
one? Well, here it is.
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<codesnippet format="1.0.0">
<Header>
<Title>ifelse</Title>
<shortcut>ifelse</shortcut>
<description>Code snippet for if...else statement</description>
<author>BotCar</author>
<snippettypes>
<snippettype>Expansion</snippettype>
<snippettype>SurroundsWith</snippettype>
</snippettypes>
</Header>
<snippet>
<declarations>
<literal>
<id>expression</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
</declarations>
<![CDATA[if ($expression$)
{
$selected$ $end$
} else { }]]>
If...else Block with Multiple Conditions
Sometimes when you write an if
statement, you have to use multiple conditions (for instance, to check a bunch of object for null
). This snippet is intended to save some time by providing placeholders for ten conditions.
<codesnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<codesnippet format="1.0.0">
<Header>
<Title>ifelse_multicondition</Title>
<shortcut>ifelse_multicondition</shortcut>
<description>Code snippet for if...else
statement with multiple conditionals</description>
<author>BotCar</author>
<snippettypes>
<snippettype>Expansion</snippettype>
<snippettype>SurroundsWith</snippettype>
</snippettypes>
</Header>
<snippet>
<declarations>
<literal>
<id>expression0</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression1</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression2</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression3</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression4</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression5</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression6</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression7</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression8</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
<literal>
<id>expression9</id>
<tooltip>Expression to evaluate</tooltip>
<default>true</default>
</literal>
</declarations>
<![CDATA[if (($expression0$) &&
($expression1$) &&
($expression2$) &&
($expression3$) &&
($expression4$) &&
($expression5$) &&
($expression6$) &&
($expression7$) &&
($expression8$) &&
($expression9$))
{
$selected$ $end$
} else { }]]>