Home > C#, ReSharper, WPF > Property declaration snippets for ReSharper

Property declaration snippets for ReSharper

March 21st, 2008

Automatic properties in .NET 3.5 are a nice thing, but they are out of the equation if you want to take advantage of the almighty INotifyPropertyChanged interface which plays a crucial role in WPF. Here’s a set of ReSharper snippets that simplify interface implementation and property declaration.

Event Declaration

The first snippet just prints out an interface implementation and adds some additional value to it, including a runtime check of property names in Debug builds (credits go to Josh Smith for this one). Just type inpc to print out this statement:

#region INotifyPropertyChanged event

///Occurs when a property value changes.
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Raises the <see cref="PropertyChanged"/> event for
/// a given property.
/// </summary>
/// <param name="propertyName">The name of the changed property.</param>
protected void OnPropertyChanged(string propertyName)
  //validate the property name in debug builds

  if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

/// <summary>
/// Verifies whether the current class provides a property with a given
/// name. This method is only invoked in debug builds, and results in
/// a runtime exception if the <see cref="OnPropertyChanged"/> method
/// is being invoked with an invalid property name. This may happen if
/// a property's name was changed but not the parameter of the property's
/// invocation of <see cref="OnPropertyChanged"/>.
/// </summary>
/// <param name="propertyName">The name of the changed property.</param>
private void VerifyProperty(string propertyName)
  Type type = this.GetType();

  //look for a *public* property with the specified name
  PropertyInfo pi = type.GetProperty(propertyName);
  if (pi == null)
    //there is no matching property - notify the developer
    string msg = "OnPropertyChanged was invoked with invalid property name {0}: ";
    msg += "{0} is not a public property of {1}.";
    msg = String.Format(msg, propertyName, type.FullName);



…and don’t forget to declare the INotifyPropertyChanged implementation. The snippet won’t do that for you:

public class Class1 : INotifyPropertyChanged


Property Declaration

The second snippet (shortcut is pcp) simplifies the declaration of a given property for you. You can define property name, type, default value and a description summary. The generated code for a sample property looks like this:

#region UserId

/// <summary>
/// The numeric ID of the user. Defaults to null.
/// </summary>
private int? userId = null;

/// <summary>
/// The numeric ID of the user. Defaults to null.
/// </summary>
public int? UserId
  get { return userId; }
    //ignore if values are equal
    if (value == userId) return;

    userId = value;



Grab the snippet here: Download

Author: Categories: C#, ReSharper, WPF Tags: , ,
  1. March 25th, 2008 at 11:22 | #1

    I like it!

    Checking the propertyName in the PropertyChanged event is one of the reasons I created the automatic class tester that tests properties and that they implement INotifyPropertyChanged properly. Hopefully a useful alternative if you use unit testing at all:


    Josh Twist

  2. March 26th, 2008 at 12:35 | #2

    Thanks for the link – the framework sure makes a nice addition to the toolbox 🙂

  3. May 26th, 2011 at 08:43 | #3

    How do I add this to Resharper?

  1. No trackbacks yet.