Working with C# Code Snippets                                                        
•        Visual Studio 2005 supports a technology dubbed code snippets, which are the foundation for two
related code-generation techniques:
•        Expansion Templates: The ability of the IDE to generate type definitions, member definitions, and
common coding constructs.
•        Surround With IntelliSense: The ability of the IDE to surround a group of selected code statements
within a relevant coding construct.
•        The whole point of code snippets is to increase developer productivity while pounding on the
keyboard.  For example:
•        Insert a foreach skeleton, new type definition, etc.
•        Wrap a chunk of code within a “region”, namespace scope, etc.
•        Code snippets can be activated by right clicking anywhere in a code window.
•        Once you select your category, you will find a list of choices.




•        You can also type the name of the snippet and hit the Tab key twice to insert the snippet:

•        In either case, once you insert a code snippet, you may find various ‘placeholders’ which you can Tab
through to fill in the blanks.
•        Each placeholder is highlighted.
•        May represent variable names, etc.


•        Here is a sampling of some (but not all) of the interesting Surround With snippets:
Surround With Snippet        Meaning in Life
#if
#region        These options allow you to wrap code with various C# preprocessor directives.
do
while
foreach
for – iteration by index
Reverse for – iteration by index         These options wrap code within various C# looping constructs.
if statement
else         Wraps a code block within if/else logic.
lock         Wraps a code block within a lock scope.
namespace         Wraps a collection of types (or namespaces) into a new namespace.
Try Catch
Try Finally         Wraps a block of code within structured exception logic.
Using        Wraps a statement inside a C# 'using scope' to ensure object disposal.

•        Here are some common Expansion Templates:
Expansion Template Snippet        Meaning in Life
class
interface
enum
struct        These options generate an empty type definition.
Named Iterator / Indexer pair        Iterators are constructs that allow you to build custom collections that
expose their sub-objects using an array like index syntax.
Basic attribute implementation         Creates a new custom .NET attribute definition (according to best
practices).
Destructor         Stubs out a C# destructor (which is an overridden System.Object.Finalize in disguise).
Exception type        Creates a definition for a custom exception (according to best practices).
Override System.Object.Equals        Overrides the virtual Equals() method inherited from System.Object.
Property
Propertyg        Defines a read/write property (property) or read-only property definition (propertyg).


Code Snippets Under the Hood                                                        
•        Code snippets are XML based descriptions of a common set of code statements:
•        Very simple code snippets define static code data.
•        XML code snippets can also define optional placeholders which the developer can supply once
inserting the snippet.
•        Given that snippets are simple XML, developers can easily build their own custom code snippets:
•        Once developed, custom snippets can be registered with Visual Studio 2005.
•        At that point, they can be activated just like any of the canned snippets.
•        Under Visual Studio 2005, all code snippets are represented by *.snippet files which are registered
under the following directory:
C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#

•        For illustrative purposes, let’s create a C# custom snippet named ‘new web method’.
•        Once activated, the snippet will dump a block of code for a .NET web method.
•        As well we will have a place holder which allows the developer to enter the return type, method name
and description.

•        In the following *.snippet file, note:
•        The code itself is within a CDATA section.
•        Placeholders are defined within a <Literal> scope and referenced by encasing the literal within $ tokens.
<CodeSnippet Format="1.0.0">
<Header>
<Title>new web method</Title>
<Shortcut>newWM</Shortcut>
<Description>Expansion snippet to generate a new Web Method
</Description>
<SnippetTypes>
  <SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
  <Literal>
    <ID>retval</ID>
    <ToolTip>return value</ToolTip>
    <Default>void</Default>
  </Literal>
  <Literal>
    <ID>methodname</ID>
    <ToolTip>method name</ToolTip>
    <Default>MyWebMethod</Default>
  </Literal>
  <Literal>
    <ID>desc</ID>
    <ToolTip>Description of Web Method</ToolTip>
    <Default>This is my Web Method</Default>
  </Literal>         
</Declarations>
<Code Language="csharp" Format="CData">
  <![CDATA[
  [WebMethod(Description="$desc$")]
  public $retval$ $methodname$()
  {
    $end$
  }
  ]]>
</Code>
</Snippet>
</CodeSnippet>

•        As mentioned, developers are free to author their own XML code snippets.
•        Full documentation of the process can be found in the article Investigating Code Snippet Technology
(www.msdn.com or the .NET 2.0 Framework Documentation).
Code Snippets
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials