Our Blog

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


Roslyn – is a recently released .NET Compiler Platform for .NET 4.6+   There is plenty of information on the web that describes this platform and where it can be utilized.
.NET Compiler Platform, better known by its codename "Roslyn", is a setof open-source compilers and code analysis APIs for C# and Visual Basic .NET languages from Microsoft.

The project notably includes self-hosting versions of the C# and VB.NET compilers – compilers written in the languages themselves. The compilers are available via the traditional command-line programs but also as APIs available natively from within .NET code. Roslyn exposes modules for syntactic (lexical) analysis of code, semantic analysis, dynamic compilation to CIL, and code emission.

Traditionally, compilers are black boxes -- source code goes in one end, magic happens in the middle, and object files or assemblies come out the other end. As compilers perform their magic, they build up deep understanding of the code they are processing, but that knowledge is unavailable to anyone but the compiler implementation wizards. The information is promptly forgotten after the translated output is produced.

For decades, this world view has served us well, but it is no longer sufficient. Increasingly we rely on integrated development environment (IDE) features such as IntelliSense, refactoring, intelligent rename, “Find all references,” and “Go to definition” to increase our productivity. We rely on code analysis tools to improve our code quality and code generators to aid in application construction. As these tools get smarter, they need access to more and more of the deep code knowledge that only compilers possess.This is the core mission of the .NET Compiler Platform (“Roslyn”): opening up the black boxes and allowing tools and end users to share in the wealth of information compilers have about our code. Instead of being opaque source-code-in and object-code-out translators, through the .NET Compiler Platform (“Roslyn”), compilers become platforms—APIs that you can use for code related tasks in your tools and applications.

The transition to compilers as platforms dramatically lowers the barrier to entry for creating code focused tools and applications. It creates many opportunities for innovation in areas such as meta-programming, code generation and transformation, interactive use of the C# and VB languages, and embedding of C# and VB in domain specific languages.

The .NET Compiler Platform (“Roslyn”) SDK Preview includes the latest drafts of new language object models for code generation, analysis, and refactoring. We hope to include drafts of API support for scripting and interactive use of C# and Visual Basic in a future preview. This document provides a conceptual overview of the .NET Compiler Platform (“Roslyn”). Further details can be found in the walkthroughs and samples included in the SDK Preview.

 ros

References:

· Roslyn Overview

· Enabling the .NET Compiler Platform (“Roslyn”) in ASP.NET applications

About Us

Web/Mobile Solutions

Our Contacts

Cincinnati, OH 45069