为什么.NET 4.6的具体code编译当目标框架的旧版本? [英] Why Does .NET 4.6 Specific Code Compile When Targeting Older Versions of the Framework?

查看:310
本文介绍了为什么.NET 4.6的具体code编译当目标框架的旧版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目标较旧版本的.NET框架(.NET 4.5.2)的一个项目。我安装了Visual Studio 2015年(以及.NET 4.6我的机器上)。我注意到,如果我用发布.NET 4.6的C#语言功能/ C#6,但它仍然编译。如果我的项目的目标框架为< .NET 4.6,应该不是这个不能编译:

I have a project that targets older versions of the .NET framework (.NET 4.5.2). I installed Visual Studio 2015 (and therefore .NET 4.6 on my machine). I noticed that if I use C# language features released in .NET 4.6/C# 6, it still compiles. If my project's target framework is < .NET 4.6, shouldn't this not compile:

    public string MyExpressionBodyProperty => "1";

    //auto properties are new in C# 6
    public string MyAutoProperty { get; } = "1";

    private static void MethodThatUsesNameOf(string filename)
    {
        if (filename == null) 
        {
            //nameof was released in C# 6
            throw new ArgumentException("The file does not exist.", nameof(filename));
        }
    }

我如何能确保我只使用了与我针对的框架版本工作的.NET语言的特点?

How can I ensure I'm only using .NET language features that work with the framework version I'm targeting?

推荐答案

.NET Framework版本和C#语言的版本是不同的东西。 C#的6语言功能

The .NET Framework version and the C# language version are different things. The C# 6 language features

public string MyAutoProperty { get; } = "1";
nameof(filename)

可以通过VS 2015年被编入code表示针对早期框架。

can be compiled by VS 2015 into code that targets earlier frameworks.

我如何能确保我只使用了与我针对的框架版本工作的.NET语言的特点?

How can I ensure I'm only using .NET language features that work with the framework version I'm targeting?

如果您尝试使用.NET 4.6框架的功能,你会得到一个合适的编译错误,如果你的目标更早的框架。

If you try to use a .NET 4.6 Framework feature, you will get an appropriate compiler error if you are targeting an earlier framework.

我将能够部署此code到机器,没有.NET 4.6?

Would I be able to deploy this code to machine which did not have .NET 4.6?

是的,你会的。只要部署机具有与所述一个你在VS2015目标兼容的框架。

Yes, you would. As long as the deployment machine has a framework compatible with the one you target in VS2015.

这篇关于为什么.NET 4.6的具体code编译当目标框架的旧版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆