Elegantly Handle Repetitive Property Code in C#
- by Eric J.
The language shortcut
public string Code
{
get;
set;
}
saves a bit of typing when defining trivial properties in C#.
However, I find myself writing highly repetitive, not-quite-as-trivial property code that still follows a clear pattern e.g.
public string Code
{
get { return code; }
set
{
if (code != value)
{
code = value;
NotifyPropertyChanged("Code");
}
}
}
I can certainly define a Visual Studio snippet to reduce typing. However, if I need to add something to my pattern, I have to go back and change quite a bit of existing code.
Is there a more elegant approach? Is a snippet the best way to go?