请看下面Go()方法中的四行代码:
delegate void Action<T>(T arg);
delegate void Action();
void DoSomething<T>(Action<T> action)
{
//...
}
void DoSomething(Action action)
{
//...
}
void MyAction<T>(T arg)
{
//...
}
void MyAction()
{
//...
}
void Go<T>()
{
DoSomething<T>(MyAction<T>); // throws compiler error - why?
DoSomething(new Action<T>(MyAction<T>)); // no problems here
DoSomething(MyAction); // what's the difference between this...
DoSomething(new Action(MyAction)); // ... and this?
}
请注意,第一次调用生成的编译器错误为: “无法从使用中推断出方法‘Action(T)’的类型参数。请明确指定类型参数。”
void DoSomething(Action action)
{
//...
}
因为 T 没有被声明... 这整段代码是不是在一个泛型类中? - Mehrdad Afshari