How to Enable C# language Versioning – Guidelines

C language version

In this article, we will understand how to build your .NET projects targeting a specific version of C#.

We shall see how to Enable C# Language Version to use the latest or any specific versions.

You may want to use a specific version depending on your need like you may want to leverage the latest C# feature from C# 8.0 etc.

Recently I happen to install the latest .NET Core version and found build on the local machine started failing for some below common errors,

 The modifier ‘public’ is not valid for this item in C# 7.3. Please use the language version 8 or greater. 
 Feature ‘using declarations’ is not available in C# 7.3. Please use language version 8.0 or greater 

A similar error also starts popping up on the build server using MSBuild commands for building the applications.

Getting started

Before understanding the C# language versioning, let’s see how to see the C# language version available on the developer or build machine.

How to Verify C# Version

Using Visual studio Command prompt:

csc /langversion:?

change c language version

As shown above, the C# compiler determines a default language version based on your project’s target framework.

The target project when built gets the highest compatible language version by default as per this versioning semantics.

I have a .NET Core-based project which targets to C# 8.0 version, which is the latest and also a default. (as shown above figure)

Please note that this default Vs the latest c# version combination may vary on each machine.

Guidelines

As per Microsoft

Moving forward, however, each version of each framework will have a single supported and default version, and we won’t support arbitrary versions.

To reflect this change in support, this commit permanently disables the language version combo box and adds a link to a document explaining the change.

So if using .NET or .NET Core then the framework will always target only a single language version and which will also be a default version.

The default version will be a major version of C#.

Override a default C# version

To override a default, you must specify the C# version explicitly.

Below are a few techniques that can be used to target specific or generic versions.

Change C# version using Edit Project File

Please set the language version in your project file.

Please explicitly instruct the compiler that you would like to use the specific version of the language.

Using C# ‘latest’ version

You can target the latest version as below,

how to target latest C version

Using C# ‘preview’ version

If you are targetting any preview framework then the language version can be targeted as a preview.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>
</Project>

Using a Specific version

One can target a specific version Example: ‘8.0’ as shown below,

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <LangVersion>8.0</LangVersion>
  </PropertyGroup>
</Project>

Change C# Language version – Configuring for Multiple projects

You can target multiple projects to use the specific or preview or latest version of the C# language version using Directory.Build.props file.

Please update the file below with the source repository.

Directory.Build.props file

Enable C Language Versioning

Set C# language version on Build Server

If using the MSBuild command using the property as shown below,

msbuild /property:langversion=latest

blank

As we discussed this default Vs the latest version combination may vary on each machine.

As a good practice keep the development and build environment consistent for the compiler and framework installed.

As a good practice, one can always use the latest version

Example:

<LangVersion>latest</LangVersion>

Note: For any issue please make sure to unload and re-load the Project in Visual Studio. You can even close and re-open the project for the settings to be effective.

References :

https://github.com/dotnet/project-system/pull/4923/commits/fc1f7f691851670909ed38fdfc3b4a4ac79c5e4b

That’s all, Happy Coding!!

Summary

In this post, we saw how to target the latest version of C# when building a .NET Core application. We understood that the C# compiler determines the language version based on the project’s target framework and learned a few techniques to override the default configuration.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



4 thoughts on “How to Enable C# Language Version-Guidelines

  1. Hi -I had the same .NET Core version installed on multiple machines but the issue was coming only a few machines.
    As stated above I validated the version and found different default versions on each machine was causing the issue.
    Thanks for clarifying this.Kudos to you!

Leave a Reply

Your email address will not be published. Required fields are marked *