方法参数的动态长度

Joh*_*han 1 c# asp.net webforms

Page.aspx:

<a href="#" <%= ToggleUiVisibility(new List<bool>() { true, true, false }) %> >
    link
</a>
Run Code Online (Sandbox Code Playgroud)

C#:

public string ToggleUiVisibility(List<bool> conditions)
{
    return conditions.Any(x=>!x) ? "style=\"display:none;\"" : string.Empty;
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以做类似的事情(伪代码)

ToggleUiVisibility(true, true, argsN);

public string ToggleUiVisibility(args)
{
    // make sure each argument is a bool and trigger the same 
    // functionality as above
}
Run Code Online (Sandbox Code Playgroud)

gza*_*axx 5

使用params关键字:

public string ToggleUiVisibility(params bool[] values)
{
    // make sure each argument is a bool and trigger the same
    // functionality as above
}
Run Code Online (Sandbox Code Playgroud)

然后你可以调用你的方法,如:

ToggleUiVisibility(true, false, ...);
Run Code Online (Sandbox Code Playgroud)