简体   繁体   English

命名和可选参数适用于.NET 2.0

[英]Named and Optional Parameters work in .NET 2.0

I created a console project in Visual Studio 2010 with .Net Framework 2.0 selected 我在Visual Studio 2010中创建了一个控制台项目,并选择了.Net Framework 2.0

namespace ConsoleApp1
{
  class Program
  {
    public int Add(int a, int b = 0, int c = 0)
    {
      Console.WriteLine("a is " + a);
      Console.WriteLine("b is " + b);
      Console.WriteLine("c is " + c);
      return a + b + c;
    }

    public static void Main()
    {
      Program o = new Program();
      Console.WriteLine(o.Add(10));
      Console.WriteLine(o.Add(10, 10));
      Console.WriteLine(o.Add(10, 10, 10));
      Console.WriteLine(o.Add(b: 20, a: 10));
      Console.ReadLine();
    }
  }
}

It works successfully. 它运作成功。

However the same code if I type in Visual Studio 2008, it fails!. 但是,如果我在Visual Studio 2008中输入相同的代码,则会失败!

Could anyone please help me with this issue as Named and Optional Parameters came with C#4 ? 任何人都可以帮我解决这个问题,因为C#4附带了Named和Optional Parameters吗?

This is because Named Parameters are a feature of the C# language, not the .net runtime. 这是因为命名参数是C#语言的一个特性,而不是.net运行时的特性。

Your VS2010 uses the C# 4.0 compiler, VS2008 uses C# 3.0. 您的VS2010使用C#4.0编译器,VS2008使用C#3.0。

This means you can use newer features of the language against an older runtime library. 这意味着您可以针对较旧的运行时库使用该语言的较新功能。

You can even use Linq (lamda syntax) in .Net 2.0 and VS 2010 if you implement the Linq methods your self (see the Linq Bridge project - this post also has a more indepth discussion as to how it all works). 如果你自己实现了Linq方法,你甚至可以在.Net 2.0和VS 2010中使用Linq(lamda语法)(参见Linq Bridge项目 - 这篇文章也有关于它如何工作的更深入的讨论)。

DaveShaw has explained named/optional parameters. DaveShaw解释了命名/可选参数。 You also mention (comments) contravariance and covariance - they are different: covariance and contravariance requires both compiler changes and library changes - IEnumerable<T> became IEnumerable<out T> , etc. That is why they don't work on older .NET versions even with new compilers. 你还别说(评论)逆变和协方差-它们是不同的:协变和逆变需要编译器的变化库的变化- IEnumerable<T>成为IEnumerable<out T>等。这就是为什么他们不工作在较旧的.NET版本甚至使用新的编译器。

So basically: 所以基本上:

  • if the feature you want is implemented entirely in the compiler, it will probably work on older .NET versions as long as you use a newer compiler 如果您想要的功能完全在编译器中实现,只要您使用较新的编译器,它就可能适用于较旧的.NET版本
  • if the feature you want requires BCL changes, it will probably only work on later .NET versions 如果您想要的功能需要BCL更改,它可能只适用于以后的.NET版本
    • unless that feature can actually be implemented entirely by additional libraries - in particular via extension methods. 除非该功能实际上可以完全由其他库实现 - 特别是通过扩展方法。 As an example, LINQ-to-Objects can work on older .NET versions (with newer C# versions) by adding LINQBridge ; 例如,通过添加LINQBridge ,LINQ-to-Objects可以在较旧的.NET版本(使用较新的C#版本)上工作 ; similarly, Microsoft.Bcl.Async adds types to some pre-4.5 frameworks allowing async / await to be used 类似地, Microsoft.Bcl.Async将类型添加到一些4.5之前的框架,允许使用async / await

I think you are confusing .NET versions and C# versions. 我认为你混淆了.NET版本和C#版本。 If you compile using Visual Studio 2010, you are using the C#4 compiler. 如果使用Visual Studio 2010进行编译,则使用的是C#4编译器。 That's regardless of the version of the .NET framework you're referencing. 这与您引用的.NET框架的版本无关。

The feature you're using is tied to the compiler version not the framework version, and so your code fails to compile in VS2008 (and will succeed in VS2010 regardless of target framework version). 您正在使用的功能与编译器版本而不是框架版本相关联,因此您的代码无法在VS2008中编译(并且无论目标框架版本如何,都将在VS2010中成功)。

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

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