简体   繁体   中英

calling a method with multiple default values

i was wondering, because i have a method with multiple default parameters

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>";
}

and when i call the method it i have to do it in order

reqLabel("prerequitie(s)")
reqLabel("prerequitie(s)", 12) 
reqLabel("prerequitie(s)", 12 , "blue")
reqLabel("prerequitie(s)", 12 , "blue", "Tahoma")

so my question is, is there any way to skip the first few default parameters?

Let's say i want to input only the colour, and the font-family like this:

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") 

Yes, it is possible with explicit naming:

reqLabel("Prerequisite(s)" , fontColour: "blue", fontFamily: "Tahoma")

Just note that named arguments should always be the last ones - you cannot specify positioned arguments after named. In other words, this is not allowed:

reqLabel("Prerequisite(s)" , fontColour: "blue", "Tahoma")

使用命名参数

reqLabel("prerequitie(s)", fontSize: 11)

您需要使用名称参数调用:

reqLabel("Prerequisite(s)" , fontColour: "blue" , fontFamily: "Tahoma") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM