Friday 1 April 2016

Useful New features of C# 6.0



 Introduction

With the release of Visual Studio 2015, we also received C# 6.  In this article I will tell you top useful features for you to discover in C# 6.

  • Auto-property Initializers: 
    If you want to set a default value for a property in a class, you do this in the constructor in C# 5 or less. C# 6 introduces Auto-property initializers that enable you to assign a default value for a property as part of the property declaration.
 

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; } = string.Empty;
    public string LastName { get; set; } = string.Empty;
    public string FullName { get { return string.Format("{0] {1}", FirstName, LastName); } }
    public DateTime DateCreated { get; set; } = DateTime.UtcNow;
    public DateTime BirthDate { get; set; }
    public ICollection<Qualification> Qualifications { get; set; } = new HashSet<Qualification>();
}

  • Expression Bodied members :  In C# 6, this expression can be used in the same way as the auto-property initializer, reducing the syntax down a bit:

public string FullName => string.Format("{0} {1}", FirstName, LastName);

  • Getter-only auto-properties :  
          With C# 6, you can now omit the set accessor to achieve true readonly auto implemented properties:
 



public DateTime BirthDate { get; }


  • String Interpolation : 
Rather than filling placeholders with indexes then provide an array of values to be slotted in at runtime, you provide the source of the parameter value instead, and the string.Format call is replaced by a single $sign. This is how the FullName property declaration looks when using string interpolation in the expression body instead:





public string FullName => $"{FirstName} {LastName}";

  • Null-conditional operators: 
This is very useful if you want to simplify the null checks. 


var people = new List<Person>();
var name = string.Empty;
if(people.FirstOrDefault() != null)
{
    name = people.First().FullName;
}

The above code can be shortened to the below snippet 

var people = new List<Person>();
var name = people.FirstOrDefault()?.FullName;

  • Index Initializers: 
This can be applied to index based collections like Dictionary

Dictionary<int, string> dict = new Dictionary<int, string>
{
    {1, "string1" },
    {2, "string2" },
    {3, "string3" }
};

       Can be re-written as, 

Dictionary<int, string> dict = new Dictionary<int, string>
{
    [1] = "string1",
    [2] = "string2",
    [3] = "string3"
};


Full Example:

The models Person and Qualification, has many-to-many relationship between them. 



public class Person
{
    public Person()
    {
        FirstName = string.Empty;
        LastName = string.Empty;
        DateCreated = DateTime.UtcNow;
        Qualifications = new HashSet<Qualification>();
    }
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
    public DateTime DateCreated { get; set; }
    public DateTime BirthDate { get; set; }
    public ICollection<Qualification> Qualifications { get; set; }
}



public class Qualification
{
    public Qualification()
    {
        Awardees = new HashSet<Person>();
        Name = string.Empty;
    }
    public int QualificationId { get; set; }
    public string Name { get; set; }
    public DateTime WhenAwarded { get; set; }
    public virtual ICollection<Person> Awardees { get; set; }
}

Happy programming!!

Don’t forget to leave your feedback and comments below!

No comments:

Post a Comment