可能重複:
C# 3.0 generic type inference - passing a delegate as a function parameter爲什麼不能推斷這些類型的參數?
爲什麼不能在Main
調用下面的代碼示例的類型參數推斷?
using System;
class Program
{
static void Main(string[] args)
{
Method(Action);
}
static void Action(int arg)
{
// ...
}
static void Method<T>(Action<T> action)
{
// ...
}
}
這給出了以下錯誤消息:
錯誤CS0411:用於方法
Program.Method<T>(System.Action<T>)
類型參數不能從使用推斷。嘗試明確指定類型參數。
非常感謝。已搜查但我找不到它。 –
@Pieter:達林提到的帖子有答案,並有一些很好的鏈接到更多的信息。 –
閱讀10篇文章或其他內容,但上面的文章不在其中。 –