Chapter 27. The Roslyn Compiler

C# 6.0 has a brand-new compiler, written entirely in C#. The new compiler is modular, so you can utilize its functionality in many ways besides compiling source code to an executable or library. Known as “Roslyn”, the new compiler makes it easier to write static code analysis and refactoring tools, editors with syntax highlighting and code completion, and Visual Studio plugins that understand C# code.

The Roslyn libraries can be downloaded from NuGet, and there are packages for both C# and VB. As both languages share some architecture, there are common dependencies. The NuGet package ID for the C# compiler libraries is Microsoft.CodeAnalysis.CSharp.

The source code for Roslyn is publicly available under the Apache 2 open source license. This opens up further possibilities, including morphing C# into a custom or domain-specific language. The source code is available on GitHub, at https://github.com/dotnet/roslyn.

The GitHub site also hosts documentation, examples, and walkthroughs that demonstrate code analysis and refactoring.

Warning

.NET Framework 4.6 does not ship with the Roslyn assemblies, and its version of csc.exe invokes the old C# 5 compiler. Installing Visual Studio 2015 remaps csc.exe to the C# 6 (Roslyn) compiler.

Without Visual Studio 2015, you can still programmatically invoke the compiler (and its services) if you download and reference the Roslyn assemblies. But the csc.exe tool that ships with the .NET Framework will remain ...

Get C# 6.0 in a Nutshell, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.