Specify the C# Version to use in your Projects

I always wondered how you could specify the C# version in your project.  One of the cool new features in C# 7.1 that I’ve been looking forward to is the “Async Main“.

But to be honest, I had no idea how, even when C# 7.1 is released, how I would be able to start use it and its new features.

A few days ago I stumbled upon this an issue in the Microsoft Docs repo.

The C# build system now includes a node inside a csproj file that controls which version of C# the compiler should follow.

Visual Studio

With the Visual Studio 2017.3 update being just released, I figured I’d try to figure out how to enable C# 7.1.  It’s kind of buried a bit but you can also edit your csproj by hand fairly easily, which will come next.

If you’re in Visual Studio, in your project properties under the Build Tab, click the “Advanced…” button.

 

Manual Editing

The good news is with the new csproj files, you can make this edit manually.  So if you are outside of Visual Studio, it’s as simple as defining a <LangVersion>.

Target Versions

Per the issue above, as well as you can see if you are in Visual Studio, the possible versions you can target are:

  • ISO-1
  • ISO-2
  • C# 3.0
  • C# 4.0
  • C# 5.0
  • C# 6.0
  • C# 7.0
  • C# 7.1 (beginning with .NET Core 2.0)
  • latest (latest minor version)
  • default (latest major version)

Async Main

Once I specified that I wanted to target C#7.1, I could then use the new async main, which compiles and runs.

Your Experience

How are you enjoying the new csproj format?  Have you converted any existing projects to the new format?   Please let me know on Twitter or in the comments.