Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 369
giokoguashvili Avatar asked Jun 22 '17 12:06

giokoguashvili


People also ask

How do I change the default language in Visual Studio?

The standard wayOn the tab Installed, click button Modify next to the version of Visual Studio you need the English language pack for: In the next window, select Language packs tab, tick the English language and then accept changes by clicking Modify button (right bottom corner).

How do I change the version of C# in Visual Studio?

Visual Studio doesn't provide a UI to change the value, but you can change it by editing the csproj file. The choice of default ensures that you use the latest language version compatible with your target framework. You benefit from access to the latest language features compatible with your project's target.


1 Answers

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:

  • What does the langversion switch do? article by Eric Lippert
  • Project System source code
  • Common MSBuild project properties page on MSDN, CscToolPath property in particular
like image 196
Leonid Vasilev Avatar answered Oct 22 '22 10:10

Leonid Vasilev