C# 4.0,可选参数和 params 不能一起使用
我如何创建一个同时具有可选参数和参数的方法?
static void Main(string[] args)
{
TestOptional("A",C: "D", "E");//this will not build
TestOptional("A",C: "D"); //this does work , but i can only set 1 param
Console.ReadLine();
}
public static void TestOptional(string A, int B = 0, params string[] C)
{
Console.WriteLine(A);
Console.WriteLine(B);
Console.WriteLine(C.Count());
}
How can i create a method that has optional parameters and params together?
static void Main(string[] args)
{
TestOptional("A",C: "D", "E");//this will not build
TestOptional("A",C: "D"); //this does work , but i can only set 1 param
Console.ReadLine();
}
public static void TestOptional(string A, int B = 0, params string[] C)
{
Console.WriteLine(A);
Console.WriteLine(B);
Console.WriteLine(C.Count());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您现在唯一的选择是重载 TestOptional(就像 C# 4 之前必须做的那样)。不是首选,但它会在使用时清理代码。
Your only option right now is to overload the TestOptional (as you had to do before C# 4). Not preferred, but it cleans up the code at the point of usage.
尝试
Try
这对我有用:
This worked for me: