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!

2 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

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

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.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
© 2008 - 2012 Marcus Grenängen. Drupal theme by Kiwi Themes.