I want to pass a method from type (class) like a parameter to another method, I don't know how to do this.
class Program
{
static void Main(string[] args)
{
MyMethod(Foo.MethodA);
MyMethod(Foo.MethodB);
}
public void MyMethod(???)
{
}
}
public class Foo
{
public void MethodA()
{
}
public int MethodB(int val)
{
return val;
}
}
I want to do just that
MyMethod(Foo.MethodA);
I don't want to pass method from a object but just from type (class)
FunctypeDelegate. TheFunctypes (and the similarActiontypes) that @RufusL talks about are delegates.Foo.MethodA()is a void function that has no parameters, whileFoo.MethodB()takes an int as a parameter and returns an int. They are not the same type (well, other than they both can be represented asdelegate).MethodAcan be represented by anAction, whileMethodBby aFunc<int, int>. You'd need to work very hard to implement anything useful inMyMethodsince the parameters passed in are so different