爲什麼這是不可能的可選參數和命名參數做到以下幾點:只有命名的參數使用的行動和Func鍵代表
Func<int, int, int> sum = delegate(int x, int y = 20) { return x + y; };
Action<string, DateTime> print =
delegate(string message, DateTime datetime = DateTime.Now)
{
Console.WriteLine(message);
};
sum(x: 20, y: 40);
print(datetime: DateTime.Now, message: "Hello");
情況:
Func<int, int, int> sum = delegate(int x, int y) { return x + y; };
Action<string, DateTime> print =
delegate(string message, DateTime datetime)
{
Console.WriteLine("{0} {1}", message, datetime);
};
Console.WriteLine(sum(y: 20, x: 40));
print(datetime: DateTime.Now, message: "Hello");
的情況下,只有可選參數:
Func<int, int, int> sum = delegate(int x, int y = 20) { return x + y; };
Action<string , DateTime> print =
delegate(string message, DateTime datetime = DateTime.Now)
{
Console.WriteLine("{0} {1}",message, datetime);
};
Console.WriteLine(sum(40));
print("Hello");
請說明你想達到什麼目的。 –
@Sudhakar,OP想要用可選參數(即具有默認值的參數)定義一個「Action」或「Func」。 –
可能重複的[參數動作其中T3可以是可選的](http://stackoverflow.com/questions/7690482/parameter-actiont1-t2-t3-in-which-t3-can-be-optional) –