Skip to content

ExampleUsage

Simon Cropp edited this page Jul 30, 2012 · 3 revisions

Your code

public class Person : INotifyPropertyChanged
{
  public string GivenName { get; set; }
  public string FamilyName { get; set; }
  public event PropertyChangedEventHandler PropertyChanged;
    
  public string FullName
  {
    get { return GivenName + " " + FamilyName; }
  }
}

What gets compiled

public class Person : INotifyPropertyChanged
{
  private string familyName;
  private string givenName;

  public event PropertyChangedEventHandler PropertyChanged;

  public string FamilyName
  {
    get
    {
      return familyName;
    }
    set
    {
      if (!Object.Equals(familyName, value))
      {
        familyName = value;
        OnPropertyChanged("FamilyName");
        OnPropertyChanged("FullName");
      }
    }
  }

  public string GivenName
  {
    get
    {
      return givenName;
    }
    set
    {
      if (!Object.Equals(givenName, value))
      {
        givenName = value;
        OnPropertyChanged("GivenName");
        OnPropertyChanged("FullName");
      }
    }
  }

  public string FullName
  {
    get
    {
      return GivenName + " " + FamilyName;
    }
  }

  public virtual void OnPropertyChanged(string propertyName)
  {
    var propertyChanged = PropertyChanged;
    if (propertyChanged != null)
    {
      propertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
  }
}