Share via


Visual Studio Missing Properties Code Snippets

Hi all,

CodeSnippets within Visual Studio are very handy to speed up the coding process and reduce the amount of typing that you have to do. In particular if you have to create classes with a lot of properties it can save a lot time if you can automate it. Unfortunately the standard property only creates Get/Sets but doesn't automate placing the variable names. If you add this following snippet to your folder: "C:\Users\<YourUserName>\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets\propc.snippet" you can access via Ctrl-K-X your new snippet!

 <?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propc</Title>
<Shortcut>propc</Shortcut>
<Description>Code snippet for property and backing field</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private $type$ $field$;
public $type$ $property$
{
get { return $field$;}
set { $field$ = value;}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Happy Coding!
Norman