简体   繁体   中英

Code builds on vs 2017 but not in vs 2015

After building and testing my solution on Visual Studio 2017 (target framework .NET 4.5, vs + resharper) I run it in production environment with Visual Studio 2015 (same target framework).

In this case I receive syntax error while trying to build it.

For example:

public double Frequency
{
    get => _frequency;
    set 
    {
        if (value > 0)
            _frequency = value;
    }
}

In VS 2015 I get:

" { or ; expected "

Why does this happen?

The specific feature you are using for your get definition (expression-bodied members) is specific to C# version 7.0, as detailed here .

So the reason why your code is not compiling is because VS 2015 uses C# 6.0 and VS 2017 uses C# 7.0.

You can change you get declaration to the following in order to make it compatible with C# 6.0 and the it will build in VS 2015:

get { return _frequency; }

Although I have never tried it, after a quick look around the web , it seems it would be possible for you to use C# 7.0 with Visual Studio 2015 if you would prefer that option. Then you should in theory be able to compile your code without making any changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM