基于其他参数的必需参数

Bal*_*i C 1 c# parameters

我试图根据之前选择的参数调用一个需要一些参数的方法.

用一个例子可能更好地解释了这一点:

public static void MyMethod (string p1, string p2, string p3 = "", string p4 = "")
{
}
Run Code Online (Sandbox Code Playgroud)

我想实现的是需要p4,如果p3给定.

如果我这样称呼它:

MyMethod("Hello", "World", "P3", // p4 now required as p3 given a value)
Run Code Online (Sandbox Code Playgroud)

我希望这是有道理的.谢谢.

Ali*_*tad 10

而不是参数的默认值(有自己的问题),我会使用重载:

public static void MyMethod (string p1, string p2)
{
   MyMethod(p1, p2, "", "");
}

public static void MyMethod (string p1, string p2, string p3, string p4)
{
   ...
}
Run Code Online (Sandbox Code Playgroud)