My C# 6 Favorite Features

Recently Microsoft released Visual Studio 2015 and the new C# 6 language.
I want to share with you my favorite features of C# 6.
Null conditional operator:
Every developer hate to get NullReferenceException when the application running, this means we need to write a lot of null reference checks, and if the object have nested objects, the null reference check become very unreadable.
For Example:

public TreeNode FindElementByName(TreeNode treeNode, string name)
{
    if (treeNode != null && treeNode.Name == name)
    {
        return treeNode;
    }
    if (treeNode != null && treeNode.Nodes != null)
    {
        foreach (TreeNode currentTreeNode in treeNode.Nodes)
        {
               FindElementByName(currentTreeNode, name);
        }
    }
    return null;
}

In C#6 we can check the null reference with “?” operator:

public TreeNode FindElementByName(TreeNode treeNode, string name)
{
     if (treeNode?.Name == name)
     {
            return treeNode;
     }
     if (treeNode?.Nodes != null)
     {
            foreach (TreeNode currentTreeNode in treeNode.Nodes)
            {
                FindElementByName(currentTreeNode, name);
            } 
     }
     return null;
}

The “?.” (null conditional operator) give us elegant way to check if object is null.

Name Of Operator:

When we write a WPF view model, and we have a Binding between the view and the view model, we must to raise PropertyChanged event in order to update the UI.

We can do it like:

    public class Tree :INotifyPropertyChanged
    {
        private TreeNode _root;
        public TreeNode Root 
        {
            get
            {
                return _root;
            }
            set 
            {
                if (_root != value)
                {
                    _root = value;
                    OnPropertyChanged("Root");
                }
            } 
        }

        public event PropertyChangedEventHandler PropertyChanged;

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

Passing the property name by ordinary way is simple, but the compiler not warning us about typos or spelling errors.
In C#6 we can do it like:

    public class Tree : INotifyPropertyChanged
    {
        private TreeNode _root;
        public TreeNode Root
        {
            get { return _root; }
            set
            {
                if (_root != value)
                {
                    _root = value;
                    OnPropertyChanged(nameof(Root));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
   }

In this case we let the compiler to do our job.

Auto Property Initializers:

If we want to create a read only property with only getter  method,

currently we need to write it like:

    public class WorkItem
    {
        private readonly DateTime _createdDate;
        private readonly IList _notes;
        public WorkItem()
        {
            _createdDate = DateTime.Now;
            _notes = new List();
        }
        public DateTime CreatedDate { get { return _createdDate; } }

        public IList Notes { get { return _notes; } }
    }

In C# 6 the Auto Property initializers give us very elegant way to write this code:

    public class WorkItem
    {
        public DateTime CreatedDate { get; } = DateTime.Now;

        public IList Notes { get; } = new List();
    }

Auto property feature is also useful when we want to create immutable objects.

Advertisements

About Idan Reuven

Idan is Microsoft Certified Proffesional Developer (MCPD) Idan Working As Senior Software developer. and specialize in professional application development with WPF, WCF, Entity Framework, XAML, HTML 5, Java Script , jQuery, MVC, MVVM, C#, C++, SQL Server Technologies.
This entry was posted in C# 6, WPF and tagged . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s