Using the ClassName() snippet function - an operator declaration snippet example

A previous tip of the week mentioned how to use the SimpleTypeName() snippet function.  Here’s how to use the SimpleTypeName() function alongside the ClassName() function.  The ClassName() function will automatically substitute the name of the outer class. 

For example, if you had

class Class1

{

}

You could make a snippet that used the ClassName function to automatically generate

public static Class1 operator +(Class1 one, Class1 two)

{

   throw new System.NotImplementedException();

}

Here’s the snippet to make the operator declaration used above:

<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

<Title>Addition operator</Title>

<Shortcut>addop</Shortcut>

<Description>Code snippet to create an addition operator for a class</Description>

<Author>Microsoft Corporation</Author>

<SnippetTypes>

<SnippetType>Expansion</SnippetType>

</SnippetTypes>

</Header>

<Snippet>

<Declarations>

<Literal Editable="false">

<ID>EnclosingClass</ID>

<Function>ClassName()</Function>

</Literal>

<Literal Editable="false">

<ID>NotImplementedException</ID>

<Function>SimpleTypeName(global::System.NotImplementedException)</Function>

</Literal>

</Declarations>

<Code Language="csharp"><![CDATA[

public static $EnclosingClass$ operator +($EnclosingClass$ first, $EnclosingClass$ second)

{

throw new $NotImplementedException$("Method not yet implemented");

}]]>

      </Code>

</Snippet>

</CodeSnippet>

</CodeSnippets>

Tags: VSTips
Suggest a Tip!