This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2005+

Using the Simplest Type Name in a Snippet

Code snippets are used to insert commonly-used fragments into source code files. When those snippets include type names the inserted name must be fully qualified or relative to a using directive. This can be automated with the SimpleTypeName function.

SimpleTypeName Function

Code snippets provide a useful mechanism for inserting boilerplate code into source files, potentially improving productivity. These code snippets can include replacements, which guide you through overtyping elements of the inserted snippet, and functions, which insert text that varies according to various parameters. I've described one such function previously in the article, "Inserting the Class Name in a Code Snippet".

When you create a code snippet that inserts the name of a class or structure, the exact name that's used should depend upon the context. If you have a using directive that references the namespace containing the inserted class, it is better to use the class name alone. If there is no such using directive, you would wish for the fully qualified name to be added to the code. You can have the code snippet make this determination automatically using the SimpleTypeName function.

To use SimpleTypeName, you create a literal with the Function element containing the function. SimpleTypeName requires a parameter to be specified. This is the fully qualified name of the type that you wish to use within the code of the snippet. You can then insert a placeholder with the literal's name into the snippet code. When the code snippet is used, Visual Studio checks for a suitable using directive and inserts either the simple type name or the fully qualified name automatically.

We can see this using the following snippet. This inserts a parallel for loop into a C# code file with replacements for the start and end of the loop and the loop control variable. The fourth literal uses the SimpleTypeName function to reference the Parallel class using its fully qualified name, "System.Threading.Tasks.Parallel". The result of the function replaces all $Parallel$ placeholders in the snippet when inserted into a source code file.

NB: This snippet is suitable for Visual Studio 2010 or later at it uses .NET 4.0 code. However, the use of the SimpleTypeName function only requires Visual Studio 2005 or later.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0"
    <Title>Parallel For Loop</Title>
    <Description>Inserts a parallel For loop</Description>
      <Literal Editable="false">
    <Code Language="CSharp">
      <![CDATA[$Parallel$.For($Start$, $End$, $Iterator$ =>
        // Insert Parallel Code

Inserting the Code Snippet

After registering the code snippet you can insert it by typing "pfor" and pressing the tab key twice. If there is no using directive that references System.Threading.Tasks, the code added will appear as shown below.

System.Threading.Tasks.Parallel.For(0, 100, i =>
    // Insert Parallel Code

If you add a using directive for the System.Threading.Tasks namespace, the class name no longer needs to be fully qualified. Inserting the code snippet in this situation gives the following result:

Parallel.For(0, 100, i =>
    // Insert Parallel Code

In either case the remaining functionality of the code snippet is unaffected. Placeholders will be added that guide you in replacing the starting value, ending value and the loop control variable in the lambda expression.

11 November 2011