Checking out Jouni Heikniemi’s excellent map of What’s new in .NET 4.5 I spotted this comment:

Methods can access call site info as parameter (CallerInfo)

Intrigued, some google-fu revealed that Anders Hejlberg demoed this in his session at build - new attributes [CallerFilePath], [CallerLineNumber] and [CallerMemberName], available for use on method parameters and automatically filled in by the compiler.

The immediate use that springs to mind is for the implementation of the INotifyPropertyChanged interface:

public string Name
    get { return mName; }
    set {
        if (!Equals(mName, value)) {
            mName = value;

protected void OnNotifyPropertyChanged(
    [CallerMemberName] string propertyName = "")

Note how we no longer need to pass the name of the property as a magic string to OnNotifyPropertyChanged() because the compiler will fill it in for us.

I’m sure there’ll be more inventive uses though …


blog comments powered by Disqus
Next Post
A story about Magic  10 Dec 2011
Prior Post
NUnit to the Rescue  01 Dec 2011
Related Posts
When (not) to use Var  16 Jul 2016
Semantic Types Redux  05 Jun 2016
Semantic Types in C#6  27 May 2016
Property Enhancements for C#  20 Dec 2015
Language Extensions for C#  19 May 2014
When should methods be Static?  09 Oct 2012
Of Method Naming and more  29 Sep 2012
Someone needs an intervention  16 Dec 2011
Regions in C#  16 May 2011
Lambda expressions and Block syntax  15 Apr 2011
More csharp posts »
Related Pages
December 2011 archive