delegate int delegate1(int x);
delegate int delegate2(string s);
static void Sample(delegate1 method) { Console.WriteLine("delegate1"); }
static void Sample(delegate2 method) { Console.WriteLine("delegate2"); }
static void Main()
{
Sample((int x) => 0); // OK:指定 int,匹配 delegate1
// Sample((x) => 0); // 編譯錯誤:無法推斷 x 型別,兩個多載皆滿足
}
當多載方法參數型別不同時,Lambda 參數必須顯式宣告型別以協助編譯器多載解析。
(x) => { } 或 () => { }() => default(T) 或 () => null(參考型別)核心概念:不要用 null 表示「什麼都不做」,要用空 Lambda 表達式。