简体   繁体   English

C#4.0项目定位于早期框架

[英]C# 4.0 project targeting earlier framework

Lets suppose if I have created a C# project which uses C# 4.0 features - optional parameter . 让我们假设我是否创建了一个使用C# 4.0 features - optional parameterC# project C# 4.0 features - optional parameter What will happen if I select '.Net Framework 2.0' as a target framework? 如果我选择'.Net Framework 2.0'作为目标框架,会发生什么? Will the compiler be intelligent enough to generate IL compatible with 2.0 on its own or will the Exe give runtime error when deployed on a machine that has only .Net framework 2.0 ? 编译器是否足够智能以自己生成与2.0兼容的IL,或者当部署在只有.Net framework 2.0的机器上runtime error ,Exe是否会产生runtime error

In the specific case of optional parameters, compatibility will work as default values to use are stored in the caller's assembly and not in the called assembly so compatibility with other assemblies is ensured. 在可选参数的特定情况下,兼容性将起作用,因为要使用的默认值存储在调用者的程序集中而不是存储在被调用程序集中,因此可确保与其他程序集的兼容性。 If it compiles, it will run. 如果它编译,它将运行。

Optional parameters are just a syntaxic sugar. 可选参数只是一种语法糖。 The following code compiles and run for a target framework 2.0 : 以下代码编译并运行目标框架2.0:

internal class Program
{
    public static class DummyClass
    {
        public static string Bar(int b = 10, int a = 12)
        {
            return a.ToString();
        }
    }

    private static void Main(string[] args)
    {
        Console.WriteLine("{0}", DummyClass.Bar(a: 8));

        Console.ReadKey();
    }
}

Read a full explanation by Mr Botelho 阅读Botelho先生的完整解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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