C# 方法没有重载,需要 0 个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11217681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:01:05  来源:igfitidea点击:

No overload for method, takes 0 arguments?

c#

提问by user1166981

I have:

我有:

 public static int[] ArrayWorkings()

I can call it happily with MyClass.ArrayWorkings() from anywhere. But I want to build in some extra functionality by requiring a parameter such as:

我可以在任何地方用 MyClass.ArrayWorkings() 愉快地调用它。但是我想通过需要一个参数来构建一些额外的功能,例如:

 public static int[] ArrayWorkings(int variable)

I get the error No overload for method ArrayWorkings, takes 0 arguments. Why is this?

我收到错误 ArrayWorkings 方法没有重载,需要 0 个参数。为什么是这样?

采纳答案by Ed S.

You changed the function to require one parameter... so now all of your old function calls, which passed no parameters, are invalid.

您将函数更改为需要一个参数……所以现在所有不传递参数的旧函数调用都无效。

Is this parameter absolutely necessary, or is it a default value? if it is a default then use a default parameter or an overload:

这个参数是绝对必要的,还是默认值?如果它是默认值,则使用默认参数或重载:

//`variable` will be 0 if called with no parameters
public static int[] ArrayWorkings(int variable=0)  

// pre-C# 4.0
public static int[] ArrayWorkings()
{
    ArrayWorkings(0);
}

public static int[] ArrayWorkings(int variable)
{
    // do stuff
}