Definition:
delegate void myC1(string mystring);
Method:
public static void myProc1 (myC1 method, string mystring);
How I can invokes the myC1delegate inside myProc1?
Delegate is the equivalent of function pointer in C/C++ you have to assign the method you want to invoke and it should respect the delegate signature
public class Program
{
public delegate void myC1(string myStr);
static void Main(string[] args)
{
myC1 meth = new Program().MethodToInvoke;
myProc1(meth,"test invoke");
}
public void MethodToInvoke( string str )
{
Console.WriteLine("test");
}
public static void myProc1(myC1 method, string mystring)
{
method(mystring);
//this will print test
}
}
From Microsoft Docs
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.