gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:10:38 25 4
gpt4 key购买 nike

我在 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();
}
}
}

它运行成功。

但是,如果我在 Visual Studio 2008 中键入相同的代码,它会失败!

谁能帮我解决这个问题,因为 C#4 附带了命名参数和可选参数?

最佳答案

这是因为命名参数是 C# 语言的一个特性,而不是 .net 运行时。

你的VS2010使用C#4.0编译器,VS2008使用C#3.0。

这意味着您可以针对较旧的运行时库使用语言的较新功能。

如果您自己实现 Linq 方法,您甚至可以在 .Net 2.0 和 VS 2010 中使用 Linq(lamda 语法)(请参阅 Linq Bridge 项目——这篇文章还对它的工作原理进行了更深入的讨论) .

关于c# - 命名参数和可选参数适用于 .NET 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16480237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com