[英]calling a method with multiple default values
我很想知道,因为我有一个多个默认参数的方法
private string reqLabel(string label, byte fontSize = 10, string fontColour = "#000000", string fontFamily = "Verdana" )
{
return "<br /><strong><span style=\"font-family: " + fontFamily + ",sans-serif; font-size:" +fontSize.ToString() + "px; color:"+ fontColour + "; \">" + label +" : </span></strong>";
}
当我调用方法时,我必须按顺序执行
reqLabel("prerequitie(s)")
reqLabel("prerequitie(s)", 12)
reqLabel("prerequitie(s)", 12 , "blue")
reqLabel("prerequitie(s)", 12 , "blue", "Tahoma")
所以我的问题是,有没有办法跳过前几个默认参数?
假设我想只输入颜色,字体系列如下:
reqLabel("Prerequisite(s)" , "blue" , "Tahoma")
/* or the same with 2 comma's where the size param is supposed to be. */
reqLabel("Prerequisite(s)" , , "blue" , "Tahoma")
是的,可以使用显式命名:
reqLabel("Prerequisite(s)" , fontColour: "blue", fontFamily: "Tahoma")
请注意,命名参数应始终是最后一个 - 您无法在命名后指定定位参数。 换句话说,这是不允许的:
reqLabel("Prerequisite(s)" , fontColour: "blue", "Tahoma")
使用命名参数
reqLabel("prerequitie(s)", fontSize: 11)
您需要使用名称参数调用:
reqLabel("Prerequisite(s)" , fontColour: "blue" , fontFamily: "Tahoma")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.