小编173*_*901的帖子

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

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

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

c# methods

4
推荐指数
1
解决办法
407
查看次数

标签 统计

c# ×1

methods ×1