A Brief Tour of C# 6.0
- Self-Hosted and Open Source
- Improvements to Existing Features
- New Surprises
- Exciting Times
C# 6.0, the latest version, contains many new language features, in addition to new compilers and new extension APIs. You've probably heard about the work on Roslyn (the .NET compiler platform), as well as the new language services APIs that enable developers to write diagnostics, refactorings, and other static code-analysis tools. Those services represent only a portion of what's new in C#.
In addition to the service layer and the APIs, many new features make development with C# more productive. The language additions in this release have no single overall theme, as we saw in previous releases. Rather, this release contains a number of smaller features that together make C# more concise, readable, and expressive. The end result is a richer language that makes developers more productive when reading and writing C# code. In this article, I'll provide an overview of these new features, with some examples showing how these features can enable you to express your designs more clearly.
Self-Hosted and Open Source
C# 6.0 is self-hosted, meaning that the C# compiler is itself written in C#—an important step that represents a major shift for the language team. Instead of working in C++ to write a C# compiler, they're actually writing the compiler in C#, just as the Visual Basic.NET compiler is written in Visual Basic.NET. (More about this in a moment.) Another change: You can explore the source for the C# compiler, because it's now open source. The home page for the C# compiler hosts both the C# and Visual Basic.NET compilers; they're part of the same Roslyn project. The compilers are being released under the Apache 2.0 license.
Because C# and Visual Basic.NET are both self-hosted languages, some new workflows are part of building the project:
- The project includes a NuGet package for the C# 6.0 compiler executable, because you need to have the C# compiler in order to build the C# compiler. The C# 6.0 compiler code base also makes use of the new features discussed in this article. The C# 6.0 compiler won't build with the C# 5.0 compiler in the currently released product.
- You'll need an updated xUnit test framework library. The C# compiler unit tests require new features in the xUnit test framework that are not available in the production version. That's also included in the source download.
- After you build the C# compiler on your machine, you'll need to update a registry setting so that you can run the C# compiler you built. This is because of the strong signing key that's part of the official Microsoft release for the C# compiler. When Microsoft makes an official release of the C# compiler, they strongly sign the C# compiler with Microsoft's official key. The strongly signed executable is trusted because it's strongly signed.
With your build of the compiler, you won't have the matching strong name key. Therefore, the executable you build won't have the same trust level. That's why you must update the registry key: The key modification instructs your machine to trust your compiler. All the notes are on the "Building, Testing, and Debugging page of the Roslyn website. The specific instructions may change as the project nears release, so be sure to consult that page for updates.
I'm excited about both C# and Visual Basic.NET being self-hosted, because it's an important milestone for the languages. Because the C# team is using C# all day, every day, a wealth of productivity and ideas have been released in the teams.
Let's move on to discussing the new features in the C# language. If you want to try these features yourself, you'll need to modify your .csproj file to turn on the new language features. Add the <LangVersion> element and set its value to experimental in the main property group for your project:
<PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{6EC6217D-6FB1-4022-8C2D-9A69919A2CEB}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>ArticleSample</RootNamespace> <AssemblyName>ArticleSample</AssemblyName> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <LangVersion>experimental</LangVersion> </PropertyGroup>