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.
Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
The C# build system now includes a node inside a csproj file that controls which version of C# the compiler should follow.
Visual StudioWith 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 EditingThe 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
Target VersionsPer the issue above, as well as you can see if you are in Visual Studio, the possible versions you can target are:
- 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)
3 thoughts to “Specify the C# Version to use in your Projects”
I guess you mean Visual Studio *2017*, rather than 2018 😉
Ooops! Thanks. Updated
Right now C# 7.1 is in early preview.
<LangVersion>and the properties page edit for it have been around for a long long time =) I believe if you try to do async main, you’ll get a Roslyn code fix on it which automatically sets the langversion for you. That’s true at least for some C# 7.1 features.