C# 6.0 Feature Additions

Background

  • C# 6.0 was made available by Microsoft along with the Roslyn compiler as a part of Visual Studio 2015 in July 2015.
  • Major version change has been released every 2 to 3 years. C# 5 was released mid 2012

Auto Property Initializers

C# 2.0


private string _name;
public string Name 
{ 
    get { return _name; } 
    set { _name = value; }
}

private bool _isGreat;
public bool IsGreat 
{ 
    get { return _isGreat; } 
    set { _isGreat = value; } 
}
                        

Auto Property Initializers

C# 3.0


public string Name { get; set; }

public bool IsGreat { get; set; }
                        

Auto Property Initializers

C# 3.0



private string _name = "C# 6.0";
public string Name 
{ 
    get { return _name; }
    set { _name = value; }
}

private string _isGreat = true;
public bool IsGreat 
{ 
    get { return _isGreat; }
    set { _isGreat = value; } 
}
                    

Auto Property Initializers

C# 6.0


public string Name { get; set; } = "C# 6.0";

public bool IsGreat { get; set; } = true;
                        

Auto Property Initializers

C# 6.0


public string Name { get; } = "C# 6.0";

public bool IsGreat { get; } = true;
                        

String Interpolation

Before C# 6.0


var query = String.Format("{{ UserId: ObjectId(\"{0}\") , Name: \"{1}\" }}", UserId, Name);
var cursor = await locationCollection.FindAsync(query);
                    

String Interpolation

C# 6.0


var query = $"{{ UserId: ObjectId(\"{UserId}\") , Name: \"{Name}\" }}";
var cursor = await locationCollection.FindAsync(query);
                    

String Interpolation

C# 6.0


Console.WriteLine($"Getting from {uri.AbsoluteUri} took {req.GetElapsed()}s");
                    

Static Using Statements

Before C# 6.0


using System.Console;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string [] args)
        {
            Console.WriteLine("Starting process");
        }
    }
}
                    

Static Using Statements

C# 6.0


using static System.Console;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string [] args)
        {
            WriteLine("Starting process");
        }
    }
}
                    

Static Using Statements

Before C# 6.0


using ReallyLongNamespaceThatMakesItLessReadable;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string [] args)
        {
            ReallyLongNamespaceThatMakesItLessReadable.Log("Failed writing to file!");
        }
    }
}
                    

Static Using Statements

C# 6.0


using static ReallyLongNamespaceThatMakesItLessReadable;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string [] args)
        {
            Log("Failed writing to file!");
        }
    }
}
                    

Null Conditional Operators

Before C# 6.0


int count = (list == null) ? 0 : list.Count();
                    

Null Conditional Operators

C# 6.0


int count = list?.Count() ?? 0;
                    

Null Conditional Operators

C# 6.0


Address address = locations?.FirstOrDefault(loc => loc.UserId == UserId)?.Address;
                    

Exception filters

Before C# 6.0


try 
{
    // code
} 
catch (HttpException ex) 
{
    if (ex.GetHttpCode() == 404)
    {
        Exec404Handler();
    }
    else if (ex.GetHttpCode() == 500)
    {
        Exec500Handler();
    }
}
                    

Exception filters

C# 6.0


try 
{
    // code
} 
catch (HttpException ex) where (ex.GetHttpCode() == 404)
{
    Exec404Handler();
}
catch (HttpException ex) where (ex.GetHttpCode() == 500)
{
    Exec500Handler();
}
                    

Exception filters

C# 6.0


try 
{
    // code
} 
catch (HttpException ex) where (Log(ex))
{
    // doesn't execute if Log returns false
}
                    

Lambda Expressions in Getter Properties

C# 3.0


var cities = locations.Select(location => location.City).Distinct();
                    

Lambda Expressions in Getter Properties

C# 3.0


private Guid _userId = Guid.NewGuid();

public Guid UserId  { get { return _userId; } }

public string GroupName { get; set; }

public UserKey Key 
{ 
    get { 
        return new UserKey(UserId, GroupName) 
    } 
}
                    

Lambda Expressions in Getter Properties

C# 6.0


public Guid UserId { get; } = Guid.NewGuid();

public string GroupName { get; set; }

public UserKey Key => new UserKey(UserId, GroupName); // no get!
                    

Lambda Expressions in Methods

Before C# 6.0


public string FormatAddress(string address, string city, string state)
{
    return address + " " + city + ", " + state;
}
                    

Lambda Expressions in Methods

C# 6.0


public string FormatAddress(string address, string city, string state) 
    => address + " " + city + ", " + state;
                    

To Reiterate

The use of C#6 requires Visual Studio 2015! If you are sharing code with other people who are not using Visual Studio 2015, they won't be able to compile the code.

.NET 5, Roslyn

In addition to open sourcing a lot of their tech infrastructure, Microsoft has improved their documentation, including .NET and Roslyn.

Thank you