调用具有多个默认值的方法

173*_*901 4 c# methods

我很想知道,因为我有一个多个默认参数的方法

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>";
}
Run Code Online (Sandbox Code Playgroud)

当我调用方法时,我必须按顺序执行

reqLabel("prerequitie(s)")
reqLabel("prerequitie(s)", 12) 
reqLabel("prerequitie(s)", 12 , "blue")
reqLabel("prerequitie(s)", 12 , "blue", "Tahoma")
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,有没有办法跳过前几个默认参数?

假设我想只输入颜色,字体系列如下:

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") 
Run Code Online (Sandbox Code Playgroud)

And*_*rei 9

是的,可以使用显式命名:

reqLabel("Prerequisite(s)" , fontColour: "blue", fontFamily: "Tahoma")
Run Code Online (Sandbox Code Playgroud)

请注意,命名参数应始终是最后一个 - 您无法在命名后指定定位参数.换句话说,这是不允许的:

reqLabel("Prerequisite(s)" , fontColour: "blue", "Tahoma")
Run Code Online (Sandbox Code Playgroud)