3

I know how to set the C# language version to use the latest syntax (Project > Properties > Build > Advanced > Language settings...) This always defaults to 'C# latest Major version (default)'

Is there any way to get Visual studio to default a new project's language version to a non-default value; preferably to the 'C# latest minor version (latest)'?

Effectively, I'd like latest passed to MSBUILD as the langversion parameter for any new project I create in VS2017 without having to remember to change this setting.

Edit - to address possible duplicate post about a link I already included in my original question... I don't really want to do this by altering 'Microsoft.Common.props' as suggested in the afore mentioned link because I'll have to change all my build machines to include this by default (the build machines should just run MSBuild; they should not include directives that may not be applicable to future builds) - ideally the value should be present in the csproj file.

Apologies if this is a duplicate - I have searched this site extensively, and also googled for an answer - but I haven't found an answer yet.

EDIT - NEW INFO - OCT 2019
VS2019 Has resolved this issue for me - see link. Depending on which version of the framework you use, you get a different language version target by default. This isn't quite what I was after, but it solves my problem. After lodging a problem ticket with Microsoft, it turns out that they are not willing to change VS2017 to do the same, so it's upgrade time for me!

Jay
  • 9,561
  • 7
  • 51
  • 72
  • 3
    You might be able to do this by [creating a Project Template](https://msdn.microsoft.com/en-us/library/xkh1wxd8(v=vs.110).aspx). – Matthew Watson Oct 17 '18 at 08:14
  • @SeM The duplicate you propose was already referenced in my question, I've updated my question to reflect why I don't want to solve this problem by editing the `Microsoft.Common.props` file – Jay Oct 17 '18 at 08:23
  • @MatthewWatson That sounds like a good idea - I'll give it a go a bit later today and let you know how I get on. – Jay Oct 17 '18 at 08:41
  • 1
    I've logged a feature request with microsoft about this: https://developercommunity.visualstudio.com/idea/360820/set-the-default-c-build-language-in-visual-studio.html – Jay Oct 18 '18 at 08:37
  • 1
    I've upvoted that request, since I would also like to do that. – Matthew Watson Oct 18 '18 at 10:49

2 Answers2

4

While it does not change the default for all new solutions+projects, for larger projects you are working on, you can create a Directory.Build.props file in your solution directory (or any directory you want it to apply to, could even be your C:\Users directory).

In this file, you can set all sorts of defaults for all projects:

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

This helps a lot for larger projects so that all new projects you add to the solution will have the same configuration.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Thanks for taking the time to reply, but as I indicated in my question - I specifically don't want to do this. The reason why is that I'm spinning up Docker containers in Linux to build code as needed as part of my CI process. I don't really want to have to maintain a customised image repository as the [microsoft/dotnet](https://hub.docker.com/r/microsoft/dotnet/) images change so frequently. – Jay Oct 18 '18 at 08:40
  • what are you doing in that image? are you using VS in that image or just the `dotnet` cli? creating a template for `dotnet new` is far easier than for VS – Martin Ullrich Oct 18 '18 at 09:03
  • I'm spinning up an instance of the container from the MS image, mounting a directory with source code from the host and using the dotnet cli to build my application. I just wanted it to build without having my build scripts / machines / container needing any extra configuration. I could certainly do what you are suggesting, but I'd prefer not to change a process that is tried, tested and working. For now I'll either put up with the inconvenience of setting the build language manually, or using my new template as per the accepted answer as both ways put the directive into the csproj file. – Jay Oct 18 '18 at 09:31
  • 1
    I see, thank you for the explanation, it's always interesting to see how people are using the product. – Martin Ullrich Oct 18 '18 at 11:19
2

Set the default C# Build Language Version in Visual Studio 2017

Just like Matthew commented, you could create a custom project template with that setting.

When you want to set langversion parameter for any new project, you can create the new project with your custom project template, otherwise, use the default template.

Note: If you just set this setting for one solution, not multiple solutions, Martin's advice is also a good choice.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • It's not ideal, but it works. I've also raised a [feature request](https://developercommunity.visualstudio.com/idea/360820/set-the-default-c-build-language-in-visual-studio.html) with MS to add this functionality to Visual Studio. – Jay Oct 18 '18 at 08:41