Hi,

In this post I will gather my Visual Studio code snippets

First a snippet for creating a property and backing field, and raising OnPropertyChanged when the backing field has changed.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>proppropertychanged</Title>
			<Shortcut>proppropertychanged</Shortcut>
			<Description>Code snippet for property and backing field, and raising OnPropertyChanged when the backing field has changed</Description>
			<Author>Christian Klint Baltz Nielsen</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 
		{ 
			if($field$ != value)
			{
				$field$ = value;
				OnPropertyChanged(() => $property$);
			}
		}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

It will create the following code:

private string _imageLabel;
public string ImageLabel
{
	get { return _imageLabel; }
	set
	{
		if (_imageLabel != value)
		{
			_imageLabel = value;
			OnPropertyChanged(() => ImageLabel);
		}
	}
}

Another one for creating a 'lazy loading property' or 'self initializing property'

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>proplazyload</Title>
            <Shortcut>proplazyload</Shortcut>
            <Description>Code snippet for property and backing field, with initializing backing field if its null</Description>
            <Author>Christian Klint Baltz Nielsen</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 
		{ 
			if($field$ == null)
			{
				$field$ = new $type$();
			}
			return $field$;
		}
    }
    $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

It will create the following code:

private string _imageLabel;
public string ImageLabel
{
    get
    {
        if (_imageLabel == null)
        {
            _imageLabel = new string();
        }
        return _imageLabel;
    }
}

 

Very often I call my return variable 'result', so a snippet that 'return result' would be nice for me. Here it is:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>if</Title>
			<Shortcut>rr</Shortcut>
			<Description>Code snippet for returning result</Description>
			<Author>Christian Klint Baltz Nielsen</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Code Language="csharp"><![CDATA[return result; $end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

and the output:

return result;

As always feel to ask or comment.