My new favorite C# language feature: [CallerMemberName]

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!

Comments

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

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

No need using expression tree to obtain anymore, Thanks!

You can do this instead of the if:

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

SneWs's picture

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

you can replace this lines


var handler = PropertyChanged;
if (handler != null)
{
    handler(this, new PropertyChangedEventArgs(propertyName));
}

with one line

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

SneWs's picture

Yep, nowadays that is a much preferred way of expressing it, but back in 2012 when I wrote this article C# did not support that syntax :)

Add 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.