C# Code Snippets for properties raising PropertyChanged event

I decided to publish the snippets I use to implement properties that raise the PropertyChanged event. There are two code snippets, one generates a property raising the event, it is called by typing propnpc in the code editor, and generates the following code:

private int myField;

 

public int MyProperty

{

    get

    {

        return this.myField;

    }

    set

    {

        if (this.myField != value)

        {

            this.myField = value;

            this.OnPropertyChanged("MyProperty");

        }

    }

}

The other generates the call to the event by typing onpc:

protected void OnPropertyChanged(string propertyName)

{

    if (this.PropertyChanged != null)

    {

        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

}

Both snippets work in Visual Studio 2008 and 2010 beta. You will need copy the two files to %USERPROFILE%\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets or %USERPROFILE%\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets folder depending of your version of Visual Studio. You can download here.

3 Comments

Comments have been disabled for this content.