C# 6.0 New Features




- November 9, 2016

Rest of the Story:

The following features require the C# 6.0 compiler which is included in Visual Studio 2015.  Remarkably however C# 6 new features does not require and updated version of Microsoft .NET Framework (i.e. as long as you compile with VS.NET 2015 the features still work against .NET Framework 4).  This is possible as the features are implemented in the compiler itself and do not have dependencies on the .NET Framework.

1.) Using Static
Using static directives for System.ConsoleColor looks like (as well as System.IO.Directory)

using static System.ConsoleColor;  
using static System.IO.Directory;  
ForegroundColor = Yellow;  
string[] files = GetFiles(directoryPath, searchPattern, System.IO.SearchOption.AllDirectories);  

These enable the invocation of numerous methods, properties and enums directly.  In each case, this eliminates the need to qualify the static member with its type.

2.) The nameof operator
This is a new contextual keyword to identify a string literal that extracts a constant for (at compile time) the unqualified name of whatever identifier is specified as an argument.  The nameof(filename) returns “filename,” the name of the Encrypt method’s parameter.  However, nameof works with any programmatic identifier.  By leveraging the nameof operator, it’s possible to eliminate the vast majority of “magic” strings that refer to code identifiers as long as they’re in scope. This not only eliminates runtime errors due to misspellings within the magic strings, which are never verified by the complier, but also enables refactoring tools like Rename to update all references to the name change identifier.

private static void Encrypt(string filename)  
  {  
    if (!Exists(filename)) // LOGIC ERROR: Using Directory rather than File  
    {  
      throw new ArgumentException("The file does not exist.",   
        nameof(filename));  
    }  
    // ...  
  }

 
3.) String Interpolation – this is an easier one to grasp.  This is the before syntax…

string.Format("Hello!  My name is {0} {1} and I am {2} years old.", person.FirstName, person.LastName, person.Age);

after…

$"Hello!  My name is {person.FirstName} {person.LastName} 
            and I am {person.Age} years old.";

The string interpolation syntax reduces errors caused by arguments following the format string that are in improper order, or missing altogether and causing an exception.

4.) Null-Conditional Operator
C# 6.0 introduces the “?.” operator known as the null-conditional operator.  The null-conditional operator translates to checking whether the operand is null prior to invoking the method or property.  What makes the null-conditional operator especially convenient is that it can be chained. If, for example, you invoke string[] names = person?.Name?.Split(' '), Split will only be invoked if both person and person.Name are not null. When chained, if the first operand is null, the expression evaluation is short-circuited, and no further invocation within the expression call chain will occur.

//after  
switch (args?.Length) {}  
//before  
(args != null) ? (int?)args.Length : null  

5.) Auto-Property Improvements
Getter-only auto-properties are a C# 6.0 feature for declaring read-only properties that are backed (internally) by a read-only field. As such, these properties can only be modified from within the constructor.  Rather than the six or so lines needed to declare a read-only property and initialize it prior to C# 6.0, now a single-line declaration and the assignment from within the constructor are all that’s needed.

public ConsoleColor ForegroundColorVerbose { get; }

A second auto-property feature introduced in C# 6.0 is support for initializers.

static private Lazy<ConsoleConfiguration>
DefaultConfig{ get; } = new Lazy<ConsoleConfiguration>(() => new ConsoleConfiguration());

6.) Expression Bodied Methods and Auto-Properties
This feature exists for both properties and methods and allows the use of the arrow operator (=>) to assign an expression to either a property or method in place of a statement body.

static public ConsoleConfiguration GetDefault() => DefaultConfig.Value;  

 
6.) Exception Improvements – ability to use a when clause to do additional filtering when an exception is thrown

try { 
        // Do stuff
    } catch (Exception e) when ((DateTime.Now.DayOfWeek == DayOfWeek.Saturday) {
        // Swallow 
    }

 
c6