你能幫我這裏,爲什麼編譯器不會推斷lambda輸入參數對嗎?C#方法覆蓋委託作爲參數,編譯器不會推斷輸入類型
例子:
void Test(Action<string> n)
{
}
void Test(Action<int,string> n)
{
}
好了,所以,當這樣做:
obj.Test(x=>{}); // compiler doesn't know x is a string
如果我這樣做:
obj.Test((x,y)=>{}); // that works, compiler know x is a int and y is a string
看來我必須指定的輸入參數類型( ?)
obj.Test((string x) => {}) // <-- Prefer not doing this
因此,編譯器無法獲得正確類型的任何原因?
謝謝! Carlos
我只能假設你有一個額外的'測試'方法,你沒有與我們分享。正如當我嘗試你的代碼時,我沒有得到任何問題,你的第一個選項,'obj.Test(x => {});'。 (vs2010,.net4) – 2010-10-01 17:05:12
是的,也許你有另一種方法'測試(動作 n)'... –
digEmAll
2010-10-01 17:15:18
嗯,這很奇怪,我使用Visual Studio 2008,檢查屏幕截圖,你能看到缺少字符串屬性的智能感知? http://img690.imageshack.us/img690/9523/overr.png – carlosdubusm 2010-10-01 17:21:16