我创建了一个控制台项目在Visual Studio 2010与.Net框架2.0选择命名和可选参数的工作在.NET 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();
}
}
}
它的工作原理成功。
但是,如果我在Visual Studio 2008中输入相同的代码,它会失败!
任何人都可以请帮我解决这个问题,因为命名参数和可选参数随C#4一起提供?
我不能完全肯定,但它可能只是编译的事情。创建的代码与'.NET 2.0'运行时完全兼容。和VS2008不理解这个构造,因为它不了解最新的'C#'规范(除非你安装额外的软件包) – Pako 2013-05-10 10:31:43
我试图找到确切的原因,因为我试图在泛型接口和委托中使用协变和反变量,它只能工作在.NET 4.0中。其他C#4.0功能在.NET 2.0或.NET 3.5中失败。 – 2013-05-10 10:33:04
@PrakashBhatia我在回答中提到了协变/反变化 – 2013-05-10 10:41:07