Caller Information Example (C# 5.0)

Well, just a quick reference mainly to myself about using this new feature for INotifyPropertyChanged where most of the time I had to implement this using reflection. Notice the propertyName is filled in by the compiler

 

public class ViewModelBase : INotifyPropertyChanged {

  protected void SetValue<T>(ref T field, T value, [CallerMemberName] string propertyName = "") {
    if (!Object.Equals(field, value)) {
      field = value;
      OnPropertyChanged(propertyName);
    }
  }
}
 
public class SomeControl : ViewModelBase {
  private int _someInt;
  public int SomeInt {
    get { return _someInt; }
    set { Set(ref _someInt, value); }  // Compiler maginc -> fills in "SomeInt" as propertyName
  }
}

 

if this post is malformed then go here

Thank you for your interest!

We will contact you as soon as possible.

FOR MORE DETAILS CONTACT US

Please leave your details and we’ll get back to you as soon as possible

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com
Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com