命名参数可以使你的代码更清晰
考虑这个简单的类:
class SmsUtil
{
public bool SendMessage(string from, string to, string message, int retryCount, object attachment)
{
// Some code
}
}
在 C#3.0 之前,它是:
var result = SmsUtil.SendMessage("Mehran", "Maryam", "Hello there!", 12, null);
你可以使用命名参数使此方法调用更加清晰 :
var result = SmsUtil.SendMessage(
from: "Mehran",
to: "Maryam",
message "Hello there!",
retryCount: 12,
attachment: null);