I have a Foo4 method that takes a parameter of type Func <>. If I pass a parameter of an anonymous type, I will not get an error. But if I create and pass in a delegate object that references a method with the correct signature, I get a compiler error. I can not understand why I get an error in this case.
class Learn6
{
delegate string Mydelegate(int a);
public void Start()
{
Mydelegate objMydelegate = new Mydelegate(Foo1);
//No Error
Foo4(delegate(int s) { return s.ToString(); });
//This line gives compiler error.
Foo4(objMydelegate);
}
public string Foo1(int a) { return a.ToString();}
public void Foo4(Func<int, string> F) { Console.WriteLine(F(42)); }
}
source
share