public void ZIPFiles(String SourceFilePath, string SourceFileName, int NoOfItemstoZip, string SourceFileExtension, string FileSizeLimit, string CategoryofSorting, string DestinationFilePath, String DestinationFileName, string DropBoxPath, string DropBoxFileName)
{
}
在这里我用很多参数声明函数。但是当我调用函数时我只使用了4个参数。像
ZIPFiles(SourceFilePath, SourceFileName, DestinationFilePath, DestinationFileName);
但它显示了我的错误。我不想删除函数声明中的代码。我可以为剩余的参数传递空值,但我还需要其他任何方法吗?
答案 0 :(得分:2)
您可以使用可选参数,因此在这种情况下,您的方法声明应如下所示:
{{1}}
您可以为此可选参数设置任何默认值,我只需将其设置为“”(空字符串),如果是字符串和整数,则设置为“0”。然后你可以调用它来调用这个方法。但请确保所有必需参数必须在左侧,并且所有可选参数应按顺序显示在最后(右侧)。
此外,当您知道其他参数可能属于同一类型时,您可以尝试'params',如下所示;
{{1}}
这里我将'params'设置为'String'参数的数组。
答案 1 :(得分:1)
当然,您可以使用optional parameters,
定义一个函数可选参数的默认值是其定义的一部分。 如果没有为该参数发送参数,则使用默认值。
默认值必须是以下类型的表达式之一: 如下:
表单default(ValType),其中ValType是值类型。
public void ZIPFiles(String SourceFilePath,
string SourceFileName, string DestinationFilePath,
String DestinationFileName,
string SourceFileExtension=".txt", string FileSizeLimit="2mb",
string CategoryofSorting="", string DropBoxPath="default",
int NoOfItemstoZip=0, string DropBoxFileName="default")
{
//Do your operations here
}
现在你可以调用这样的方法:
ZIPFiles(SourceFilePath, SourceFileName, DestinationFilePath, DestinationFileName);
答案 2 :(得分:0)
make int NoOfItemstoZip = 0,string parametername = null
这是在c#中定义可选参数的方法所以现在如果你传递任何值,它将采用那个,否则它将指定null
答案 3 :(得分:0)
un-lucky的答案是正确的,但是如果您认为方法确实需要这么多参数,请创建一个对象来保存这些值然后通过这个对象是你的方法。这样,您可以更好地控制要设置的默认值。