Wassim AZIRAR Wassim AZIRAR - 3 months ago 13
C# Question

using a Code Snippet for INotifyPropertyChanged

I found this code snippet for INotifyPropertyChanged

But it shows the code like this :

INotifyPropertyChanged

I would have this :


  1. for public : capital letter for the first letter + ...

  2. for private : underscore + small letter for the first letter + ...



How can I achieve this ?

Edit : Without having to type the public and the private fields

<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>
<Literal>
<ID>notifyMethod</ID>
<ToolTip>name of method to raise PropertyChanged event</ToolTip>
<Default>NotifyPropertyChanged</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[private $type$ _$property$;
public $type$ $property$
{
get { return _$property$;}
set
{
if (value != _$property$)
{
_$property$ = value;
$notifyMethod$("$property$");
}
}
}
$end$]]>
</Code>
</Snippet>

Answer

I don't think this can be done with native code snippets feature provided by Visual Studio.

Personally I use Resharper which makes it possible. It can turn code I write like

public string Name { get; set; }

into

private string _name;
public string Name
{
    get { return _name; }
    set
    {
        if(value == _name)
            return;
        _name = value;
        OnPropertyChanged("Name");
    }
}

It even generates the OnPropertyChanged() method for you.

Comments