Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to get VS2015 to validate for earlier syntax of C# to maintain compat with VS2013

A specific problem I'm running into is that VS2015 allows you to use newer C# syntax, e.g. public string MyProperty => _myProperty; when targeting a .NET 4.0 framework project and then others opening the project in VS2013 get compiler errors.

Is there a way to configure VS2015 so that it warns or errors on features specific to C# 6? Or said another way, can I tell VS2015 which version of C# I'd like to compile against?

like image 225
MikeJansen Avatar asked Dec 09 '15 21:12

MikeJansen


2 Answers

Yes, but this needs to be done on a per-project basis. It's an option available in the Build menu of the project properties. Click "Advanced" in the bottom right, then select the language version you want.

enter image description here

Note: There is a VS2015 bug (which has been fixed in Update 1) that causes false negatives on get-only auto-properties. To get the best results, make sure you have installed the latest update.

like image 193
BJ Myers Avatar answered Nov 02 '22 19:11

BJ Myers


To piggyback on the accepted answer, to quickly implement this in 100's of projects, and to get all configurations and not just a specific configuration, I mass edited the csproj files and inserted a <LangVersion>5</LangVersion> node as the first child inside the first naked, unconditional <PropertyGroup> node:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <LangVersion>5</LangVersion>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
like image 44
MikeJansen Avatar answered Nov 02 '22 20:11

MikeJansen