C# 5.0

C# 5.0 – Caller Information

visual_studio_logoOne of the new feature I really like in C# 5.0 was introduced for the very first time at the Microsoft BUILD in September 2011 in California. Microsoft presented to the world of the developers the next Windows and the APIs WinRT. Today I’ would like to talk a bit about the Caller Information. This feature consists of the use of the attributes CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute. These attributes can be use with optional parameters in a function, method, delegate, Property…

The basic usage of this feature is when you want to implement a log system in your software. By writing the following code, you can retrieve the calling member name, the number of the line of code that called this method and the file path without having to do reflection or anything by yourself.

public void Do()
{
    Log("Example");
}

public void Log(string message,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
{
    // Implement your log system }

Another cool usage is with the interface INotifyPropertyChanged. In C# 4 and before you have to write the following code in order to raise the event PropertyChanged when you update the property Name.

public class Product : INotifyPropertyChanged {

    #region Name

    private string _name;

    /// <summary>
    /// Gets or sets the Name. 
    /// </summary>
    public string Name
    {
        get { return _name; }
        set {
            if (value == _name) return;

            _name = value;

            NotifyPropertyChanged("Name");
        }
    }

    #endregion #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion }

The problem here, is when you have several properties and you want to do a basic Copy & Paste. If you’re doing it too fast, you’ could forget the line NotifyPropertyChanged(“Name”); or type a wrong property name and because it is a string value, your Visual Studio or ReSharper will not say anything, and the compilation will not crash. So in C# 5.0 you can write it like that:

public class Product : INotifyPropertyChanged {

    #region Name

    private string _name;

    /// <summary>
    /// Gets or sets the Name. 
    /// </summary>
    public string Name
    {
        get { return _name; }
        set {
            if (value == _name) return;

            _name = value;

            NotifyPropertyChanged();
        }
    }

    #endregion #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion }