How to change Visual Studio 2017 default language version for all projects?
Asked Answered
P

1

44

How to set language version on C#7.0 for all projects?

Project/Properties/Build/Advanced.../Advanced Build Settings screen

Where does default parameter come from, I want to change default value

PS: I do not mean the UI language

Philippa answered 22/6, 2017 at 12:12 Comment(9)
Possible duplicate of Change language of Visual Studio 2017 RCTver
I do not mean the UI language, but the language of CodePhilippa
blogs.msdn.microsoft.com/benjaminperkins/2017/03/23/… look, this may helpBitolj
I builded project on C#7, for this I changed Language version per project. But I want change the default value of VS, for build any project without manual settings.Philippa
If you use ReSharper you could do as in the second answer here: #30462140Intersidereal
Can't believe they still don't have an option for this!Collected
Setting a language version for the solution wouldn't make much sense because you may have different languages in it (TypeScript, JS, F#, C++, VB.NET, you name it). Hence you have to configure it per project. :(Peregrine
github.com/khellang/LangVersionFixer you may also want to take a look at this toolPeregrine
The answers here cover the right™ way of doing things, however, you could also create a custom project template with exactly the config you want and use the for new projects instead of the template you currently use. Would give you complete control, but would add a maintenance cost (you need to keep updating your template if you want the latest features/tweaks/libraries/???)Ramin
O
41

The meaning of default value comes from the C# compiler itself. So in order to change it's meaning you need to change the compiler.

But in compiler that comes with Visual Studio 2017 default actually means C# 7.0, so you don't need to do anything.

Visual Studio Project System merely passes language version value to MSBuild. And MSBuild passes it further to C# compiler as a /langversion option. /langversion option lets you specify an upper language version that compiler accepts. In another words it allows you to restrict language features usage to a certain version. If you use feature from language version higher than you specified, C# compiler will emit error. That's all. If you specify /langversion as default, C# compiler will accept all valid syntax that last major C# language version include (see /langversion (C# Compiler Options) page on MSDN). Last major C# version that comes with Visual Studio 2017 is 7.0. See Features Added in C# Language Versions page on C# GitHub repository.

If you need to enable features of latest minor versions (7.1, 7.2 etc.) or forbid usage of some new C# features for multiple projects or solutions at once you can use MSBuild 15 Directory.Build.props customization file. Relevant excerpt from Customize your build article on MSDN:

...now you can add a new property to every project in one step by defining it in a single file called Directory.Build.props at the root of your repo. When MSBuild runs, Microsoft.Common.props searches your directory structure for the Directory.Build.props file (and Microsoft.Common.targets looks for Directory.Build.targets). If it finds one, it imports the property. Directory.Build.props is a user-defined file that provides customizations to projects under a directory.

Following example of a Directory.Build.props file instructs C# compiler to accept all valid syntax of a latest minor C# version (C# 7.2 in Visual Studio 2017 version 15.5.3) in all projects given their .csproj file doesn't include <LangVersion> tag which takes precedence:

<Project>
    <PropertyGroup>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>
</Project>

For more information check:

Outlet answered 1/12, 2017 at 14:13 Comment(6)
My VS17 defaults to both Framework 4.6.1 and "C# latest major version (default)". If I want to use ValueTuples (without nuget) I need to manually change the Framework version to 4.7 which is OK. But now if I want to use c# 7.2 features (the in modifier or readonly struct) the project won't compile unless I change the Language version to "C# latest minor version (latest)" which I then have to do for each build configuration. Is there not a way to default both to the latest? You seem to imply that it already should (for the langversion at least) but that doesn't seem to be the caseFelisafelise
You can set default .NET Framework version for all new projects by changing it in New Project dialog box. For all existing projects you have to change it by hand in UI or in .csproj files directly.Outlet
default language version stands for last major version, which is 7.0 in compiler that comes with Visual Studio 2017. latest stands for last minor version. In compiler that comes with Visual Studio 2017 15.5 it means 7.2. See Visual Studio 2017 version 15.5 Release Notes. You can change it for all build configuration by choosing All Configurations in Configuration dropdown in project properties or by placing <LangVersion>latest</LangVersion> in a non-conditional <PropertyGroup> tag in .csproj file.Outlet
I didn't even realize the Framework version was in the New Project dialog so thanks for that. I completely forgot about the 'All Configurations' "configuration" which is definitely helpful here. I was still looking for a way to default to "latest" (minor) for all new projects without having to do anything manually (other than the first time, that is). I found that you can edit Microsoft.Common.props to include the PropertyGroup OR you could do it in each of the "template" projects (though that would be a lot of work) but I don't know if those will be overwritten during updatesFelisafelise
Try MSBuild 15 Directory.Build.props customization file. See Customize your build article on MSDN. Use <LangVersion>latest</LangVersion> and <TargetFramework>net47</TargetFramework> tags.Outlet
Great! As my solution has 200+ projects, this helped a lot to set version to 7.1!Walkway

© 2022 - 2024 — McMap. All rights reserved.