Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

Shortcut to generate different property snippets in Visual Studio

5.00/5 (1 vote)
13 Jan 2011CPOL 9.1K  
I ended up adding one for a Lazy Property: propLazy ...
I ended up adding one for a Lazy Property:

XML
<?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:

C#
private System.Lazy<string> _MyProperty = new System.Lazy<string>();
public string MyProperty { get { return _MyProperty.Value; } set { _MyProperty = new Lazy<string>(() => value); } }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)