My new favorite C# language feature: [CallerMemberName]

SneWs's picture

With the release of Visual Studio 2012 and C# 4.5, [CallerMemberName] makes its entrance. With this new awesome feature you can write INotifyPropertyChanged code without having to worry about renaming properties and keeping strings in sync for the call to OnPropertyChanged(…).

 

This is how a property with INotifyPropertyChanged support looks like pre C# 4.5:

public string OldStylePropertyChanged
{
	get { return _oldStyle; }
	set
	{
		if (value != _oldStyle)
		{
			_oldStyle = value;
			OnPropertyChangedOldStyle("OldStylePropertyChanged");
		}
	}
}
private void OnPropertyChangedOldStyle(string propertyName)
{
	var handler = PropertyChanged;
	if (handler != null)
	{
		handler(this, new PropertyChangedEventArgs(propertyName));
	}
}

And this is how it will look like when we apply the use of [CallerMemberName] in our PropertyChanged method:

public string DynamicTitle
{
	get { return _dynamicTitle; }
	set
	{
		if (value != _dynamicTitle)
		{
			_dynamicTitle = value;
			OnPropertyChanged();
		}
	}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
	var handler = PropertyChanged;
	if (handler != null)
	{
		handler(this, new PropertyChangedEventArgs(propertyName));
	}
}

Some more really cool additions to [CallerMemberName] is the following Parameter attributes:

[CallerMemberName]
[CallerFilePath]
[CallerLineNumber]

The name of the attributes is straight forward and easy to understand, so I doubt that I would have to describe them in detail, instead, let’s look at an example straight from MSDN on how to use them.

public sealed class Logger
{
    public void TraceMessage(string message,
                             [CallerMemberName] string memberName = "",
                             [CallerFilePath] string sourceFilePath = "",
                             [CallerLineNumber] int sourceLineNumber = 0)
    {
        Debug.WriteLine("message: " + message);
        Debug.WriteLine("member name: " + memberName);
        Debug.WriteLine("source file path: " + sourceFilePath);
        Debug.WriteLine("source line number: " + sourceLineNumber);
    }
}

Happy hacking!

5 comments

by Asghar Panahy on Tue, 01/22/2013 - 16:28

This is very nice feature. Thanks for provided example. I guess I'm gonno fall in love (again)

by Dmitra on Sun, 06/09/2013 - 14:28

Отличный сайт у вас, побольше бы таких.

by Donald on Sat, 09/19/2015 - 07:13

No need using expression tree to obtain anymore, Thanks!

by Joel on Wed, 06/15/2016 - 22:03

You can do this instead of the if:

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

SneWs's picture

by SneWs on Mon, 06/27/2016 - 21:13

C# didn't have support for that feature. And yes, you are correct sir! Nowadays that would be the preferred way of expressing it :)

Post new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
© 2008 - 2012 Marcus Grenängen. Drupal theme by Kiwi Themes.