C#函数和可选参数 [英] C# functions and optional parameters
本文介绍了C#函数和可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道在C#中可以定义可选参数.我的问题是关于它的灵活性.
I know that in C# it is possible to define optional parameters. My question is directed at how flexible this is.
让f具有以下功能,其中 a 是必需的,而 b , c 是可选的:
Let f be a function as below, with a mandatory and b, c optional :
class Test {
public void f(int a, int b = 2, int c = 3) {
//...
}
}
现在,我知道我可以通过以下方式调用该函数:
Now, I know I can call the function in the following ways :
f(1)-> a等于1,b等于2,c等于3
f(1) -> a equals 1, b equals 2, c equals 3
f(11,22)-> a等于11,b等于22,c等于3
f(11,22) -> a equals 11, b equals 22, c equals 3
f(11,22,33)-> a等于11,b等于22,c等于33
f(11,22,33) -> a equals 11, b equals 22, c equals 33
如何不指定 b ,而是指定 a 和 c ?
How do I do to not specify b, but a and c ?
推荐答案
尝试:
f(11, c: 33)
并查看文档
这篇关于C#函数和可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文