New feature in C# 5.0 – [CallerMemberName]


By releasing the beta version of Visual Studio 11 and .NET 4.5, Microsoft has also released the new version C# 5.0. C# 5.0 brings a new programming pattern called asynchronous programming with new keyworkds async an await. I have blog posted about it previously. C# 5.0 also brings new features which accelerate development specialy when you implementing bindable properties, and need to put OnPropertyChanged with property name as an argument in each setter block.

C# 5.0 introduce [CallerMemberName] attribute which you can put as an agrument of the method. It is very strange syntax for C# but very efficient. For example:

/// <summary>
/// Notifies when the property is changed
/// </summary>
/// <param name="propertyName">New Features in C# 5, that you can easely skip writing string of property name.</param>
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    //whatever you need to be called
}

The method above you can call in the following situation:

public string SomeProperty
{
    get
    {
        return _someProperty;
    }
    set
    {
        if (value != _someProperty)
        {
            _someProperty = value;
            //You dont need to pass string of the propertyname, compiler will do for us.
          //OnPropertyChanged("SomeProperty"); - no need any more
            OnPropertyChanged();
        }
    }
}

What hepends behind the scene is that the c# compiler resolve your property name and put on the right place. If you dont beleive  you can see the picture which has taken during the debuging:

The whole example is listed here.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace NewFeatureCsharp
{
class Program
{
    static void Main(string[] args)
    {

        SomeClass someClass = new SomeClass();

        someClass.SomeProperty = "Hello World";

        Console.ReadKey();
    }

    public static void CallAfterPropertyChanged()
    {
        Console.WriteLine("Property is changed!");
    }
}

/// <summary>
/// Class example with propertychanged
/// </summary>
public class SomeClass : INotifyPropertyChanged
{
    private string _someProperty;
    /// <summary>
    /// This is property with automatic implementation of getter and setter, but in C# 5.
    /// This is also automatic implermentation of propertychanged notification as well.
    /// </summary>
    public string SomeProperty
    {
        get
        {
            return _someProperty;
        }
        set
        {
            if (value != _someProperty)
            {
                _someProperty = value;
                //You dont need to pass string of the propertyname, compiler will do for us.
                OnPropertyChanged();
            }
        }
    }

    /// <summary>
    /// Notifies that the property is changed
    /// </summary>
    /// <param name="propertyName">New Features in C# 5, that you can easely skip writing string of property name</param>
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        Program.CallAfterPropertyChanged();
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
}

And output result is the following:

Advertisement

2 thoughts on “New feature in C# 5.0 – [CallerMemberName]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s