Saeb Amini Saeb Amini - 1 month ago 10
C# Question

How to Put Empty Literal in Code Snippet

I've slightly modified VS's

ctor
snippet. The idea is that if I want parameters or a body, I type them in, otherwise I can hit Enter twice to skip and make it parameterless:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>ctor</Title>
<Shortcut>ctor</Shortcut>
<Description>Code snippet for constructor</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>Class name</ToolTip>
<Function>ClassName()</Function>
<Default>ClassNamePlaceholder</Default>
</Literal>
<Literal>
<ID>parameters</ID>
<ToolTip>Parameters</ToolTip>
</Literal>
<Literal>
<ID>body</ID>
<ToolTip>Body</ToolTip>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[public $classname$ ($parameters$)
{$body$}$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


It works great, except it has a small nuisance in case I want parameterless:

public ProjectTask( )
{ }


Notice the extra space character
between the parentheses? I know I'm taking laziness to a whole new level here but is it possible to make that go away?

Answer

I just tried my exact same snippet above in VS 2015 and noticed it no longer has that problem and the generated code ends up like this:

public ProjectTask()
{ }