-1

This code shows a property that calls out to a method to get that property's name:

public string Foo { get { return MyName(); } }

string MyName([System.Runtime.CompilerServices.CallerMemberName] 
    string propertyName = null)
{
    return propertyName;
}

Is there a better way?

Jerry Nixon
  • 30,369
  • 13
  • 111
  • 227

1 Answers1

2

That arguably is the best way as of .Net 4.5 having replaced the previous .net call such as

NotifyPropertyChanged("CustomerName");

where the user had to hard code that information. Now in .Net 4.5 the call looks like this:

NotifyPropertyChanged();

thanks to

void NotifyPropertyChanged([CallerMemberName] string propertyName = null)

See Implementing INotifyPropertyChanged - does a better way exist? for other strategies.

Community
  • 1
  • 1
ΩmegaMan
  • 26,526
  • 10
  • 91
  • 107