.NET · C#

.NET 2015: C# 6

C# in its 6th version received its major change in the language engine “the compiler” along with many other little improvements in the language features

The new compiler “Roslyn”

C#/VB teams were working for years on the new compiler Roslyn, and here is why

What is wrong with the old compiler?
  • The old compiler is some sort of black box compilation without easy way to access the source code which makes live code analysis very difficult
  • Visual Studio provides build-time static code analysis only
  • Visual Studio popular extensions such as ReSharper uses its own compiler to provide live source code analysis and fixes
  • it is too difficult for developers to write code analyzers and fixers
Roslyn to the rescue
  • Roslyn is rewritten in C#, and open sourced on GitHub.com
  • It provides an API for developers to access its internals and services
  • It becomes too much easier to develop extensions for Visual Studio using the new compiler’s APIs such as Alive and SonarLint Visual Studio extensions
  • It enables building live code analyzers and fixers which do analysis on the code by reading from the buffer, and find patterns and suggest refactoring as you go
  • Developers can use Roslyn public API to ship analyzers and fixers along with their libraries to show guidance on how their library should be used
  • Visual Studio 2015 uses Roslyn API in order to bring productivity features while coding in C# and VB

Language Improvements

The driving philosophy in the development of C# 6 was to build many small improvements almost syntactical to make every day code easier and lesser. In previous versions C# introduced some big language features such as LINQ and Dynamic Capabilities 

Below are the 2 features I liked most. For the full list of C# 6 new features read C# 6 new features article on Roslyn Wiki

Null Conditional Operator

This code

var managerName = (employee != null 
                   && employee.Department != null 
                   && employee.Department.Manager != null) 
                   ? employee.Department.Manager.Name : null;

In C# 6 it can be

var managerName = employee?.Department?.Manager?.Name;
String Interpolation

This code

var firstName = "First";
var lastName = "Last";
var fullName = string.Format("Full name is {0} {1}", firstName, lastName);

In C# 6 it can be

fullName = $"Full name is {firstName} {lastName}";

If you prefer videos over reading you can watch What’s New in C# 6

Advertisement

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 )

Facebook photo

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

Connecting to %s