Code Snippet Functions
This page is specific to:.NET Framework Version:2.03.54.0
Visual Studio
Code Snippet Functions

There are three functions available to use with Visual C# code snippets. Functions are specified in the Function Element (IntelliSense Code Snippets) element of the code snippet. For information on creating code snippets, see Creating and Using IntelliSense Code Snippets.

Functions

The following table describes the functions available for use with the Function element in code snippets.

Function

Description

Language

GenerateSwitchCases(EnumerationLiteral)

Generates a switch statement and a set of case statements for the members of the enumeration specified by the EnumerationLiteral parameter. The EnumerationLiteral parameter must be either a reference to an enumeration literal or an enumeration type.

Visual C#

ClassName()

Returns the name of the class that contains the inserted snippet.

Visual C#

SimpleTypeName(TypeName)

Reduces the TypeName parameter to its simplest form in the context in which the snippet was invoked.

Visual C#

Example

The following example shows how to use the GenerateSwitchCases function. When this snippet is inserted and an enumeration is entered into the $switch_on$ literal, the $cases$ literal generates a case statement for every value in the enumeration.

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>switch</Title> 
            <Shortcut>switch</Shortcut> 
            <Description>Code snippet for switch statement</Description> 
            <Author>Microsoft Corporation</Author> 
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType> 
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>expression</ID> 
                    <ToolTip>Expression to switch on</ToolTip> 
                    <Default>switch_on</Default> 
                </Literal>
                <Literal Editable="false">
                    <ID>cases</ID> 
                    <Function>GenerateSwitchCases($expression$)</Function> 
                    <Default>default:</Default> 
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[
                    switch ($expression$)
                    {
                        $cases$
                    }
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

The following example shows how to use the ClassName function. When this snippet is inserted, the $classname$ literal is replaced with the name of the enclosing class at that location in the code file.

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Common constructor pattern</Title> 
            <Shortcut>ctor</Shortcut> 
            <Description>Code Snippet for a constructor</Description>
            <Author>Microsoft Corporation</Author> 
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID> 
                    <Default>int</Default> 
                </Literal>
                <Literal>
                    <ID>name</ID> 
                    <Default>field</Default> 
                </Literal>
                <Literal default="true" Editable="false">
                    <ID>classname</ID> 
                    <ToolTip>Class name</ToolTip> 
                    <Function>ClassName()</Function> 
                    <Default>ClassNamePlaceholder</Default> 
                </Literal>
            </Declarations>
            <Code Language="vjsharp" Format="CData">
                <![CDATA[ 
                    public $classname$ ($type$ $name$)
                    {
                        this._$name$ = $name$;
                    }
                    private $type$ _$name$;
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

This example shows how to use the SimpleTypeName function. When this snippet is inserted into a code file, the $SystemConsole$ literal will be replaced with the simplest form of the Console type in the context in which the snippet was invoked.

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Console_WriteLine</Title> 
            <Shortcut>cw</Shortcut> 
            <Description>Code snippet for Console.WriteLine</Description> 
            <Author>Microsoft Corporation</Author> 
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType> 
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false">
                    <ID>SystemConsole</ID> 
                    <Function>SimpleTypeName(global::System.Console)</Function> 
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[ 
                    $SystemConsole$.WriteLine();
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
See Also

Concepts

Reference

Community Content

What is the general idea of this "function" thing?
Added by:netsi1964
Hi,

I have many times tried hard to find a use of Code Snippets, recently I have added a tool: http://snippetdesigner.codeplex.com/ to my Visual Studio 2008. It made it very easy to add new snippets, but I do not believe that Code Snippets have do not have more facilities than just automatic replacement of variables.

I discovered the "Function" part of Snippets, and ended here. What do I discover? Some simpel content which allows me to do 3 (!) things! What is the general idea of the Function part of a snippet? What value will it add to snippets running in an XML scope? Or other scopes for that matter...

Please inlighten me! :-)
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View