65.9K
CodeProject is changing. Read more.
Home

Shortcut to generate different property snippets in Visual Studio

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jan 12, 2011

CPOL
viewsIcon

9120

I ended up adding one for a Lazy Property: propLazy ...

I ended up adding one for a Lazy Property:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propLazy</Title>
            <Shortcut>proplazy</Shortcut>
            <Description>Code snippet for property and lazy backing field</Description>
            <Author>ME</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>string</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[private System.Lazy<$type$> _$property$ = new System.Lazy<$type$>();
public $type$ $property${ get{ return _$property$.Value; } set{_$property$ = new Lazy<$type$>(() => value);} }
    $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
which produces:
private System.Lazy<string> _MyProperty = new System.Lazy<string>();
public string MyProperty { get { return _MyProperty.Value; } set { _MyProperty = new Lazy<string>(() => value); } }