The new C# 6.0 nameof
is great in the PropertyChanged
pattern for propagating property changes using something like:
private string _myProperty;
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty= value;
OnPropertyChanged(nameof(MyProperty));
}
}
When listening of property changes I use this (yes, even with ugly hardcoded strings):
private void OnMyObjectPropertyChanged(object sender, PropertyChangedEventArgs args)
{
switch (args.PropertyName)
{
case "MyProperty":
DoSomething();
break;
}
}
With the new nameof expressions would this code compile / work?
private void OnMyObjectPropertyChanged(object sender, PropertyChangedEventArgs args)
{
switch (args.PropertyName)
{
case nameof(MyObject.MyProperty):
DoSomething();
break;
}
}
nameof(MyObject.MyProperty)
is going to work.MyProperty
is not static. You would need an instance ofMyObject
. – Gusty