Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change C# Language Version for all of the projects in my solution in one place?

I have a solution with 20 projects, I want to change the C# version for all of them to C# 7.3

Is there a way that I could change all project's version at one go? I know I can change the Language Version from Project's Properties, but I don't want to repeat this 20 times.

enter image description here

like image 551
Hooman Bahreini Avatar asked Apr 26 '20 00:04

Hooman Bahreini


2 Answers

To set a version for all your project at once, you can create a file named Directory.Build.props (case-sensitive on Linux) at the root of your repository. This file contains the list of common properties of your projects:

<Project>
  <PropertyGroup>
    <LangVersion>latest</LangVersion>
    <!--<LangVersion>preview</LangVersion>-->
    <!--<LangVersion>7.3</LangVersion>-->
  </PropertyGroup>
</Project>

https://www.meziantou.net/4-ways-to-enable-the-latest-csharp-features.htm#method-4-using-a-pro

like image 71
meziantou Avatar answered Oct 11 '22 18:10

meziantou


The accepted answer is great, I am just adding some details.

I created Directory.Build.props file at the root of the of the repository and added it to the solution as a Solution Item. This is the content of the file:

<Project>
  <PropertyGroup>
    <LangVersion>7.3</LangVersion>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
  </PropertyGroup>
</Project>

Note 1:

If you modify these setting, you need to restart Visual Studio for the changes to take effect.

Note 2:

You would require MSBuild version 15 or higher in order to use Directory.Build.props. To check MSBuild version, open your project file in a text editor and look for the following:

<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Here, 15.0 indicates your MSBuild version, see this document if you require to upgrade your MSBuild version.

like image 21
Hooman Bahreini Avatar answered Oct 11 '22 19:10

Hooman Bahreini