The modifier is not valid in C#.Use language version and greater

Issue Description

.NET build (MSBuild or Visual Studio) gives below or similar format of the error.

The modifier 'public' is not valid for this item in C# 7.3. Please use the language version 8 or greater.

Or

Feature 'default literal' is not available in C# [version]. Please use the language version  or greater. 

Build server error

The issue is most visible in .NET/.NET Core framework using incompatible C# language version.

Resolution:

C# compiler determines the language version(default) based on the project’s target framework.

The issue can be resolved using any of the below approaches.

1. 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.

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

blank

One can target specific version Example ‘8.0’ as well,

blank

2. 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 as below with the source repository.

Directory.Build.props file

blank

3. Settings on Build Server

If using a build server to build the project using MSBuild command as below,

msbuild /property:langversion=latest
blank

Why this change?

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.

This versioning also indicates that the target project when built, gets the highest compatible language version by default.

The modifier is not valid in C

If using .NET or .NET Core then this framework will always target only a single framework and which will also be a default version.

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

Example

<LangVersion>latest</LangVersion>

References :

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

Summary

In this post, we saw how to target the latest version of C# when building .NET Core application and resolve common issues related to a specific version.

Did the above steps resolve your issue? Please sound off in your comments below!



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.



2 thoughts on “Resolved: The modifier is not valid in C#. Use language version and greater

Leave a Reply

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