Elegantly Handle Repetitive Property Code in C#

Posted by Eric J. on Stack Overflow See other posts from Stack Overflow or by Eric J.
Published on 2010-05-11T04:43:06Z Indexed on 2010/05/11 4:54 UTC
Read the original article Hit count: 169

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#4.0