C# 如何在myProc1中调用Myc1委托?

C# 如何在myProc1中调用Myc1委托?,c#,.net,delegates,C#,.net,Delegates,定义: delegate void myC1(string mystring); 方法: public static void myProc1 (myC1 method, string mystring); 如何在myProc1中调用Myc1委托?委托相当于C/C++中的函数指针。您必须指定要调用的方法,并且它应该尊重委托签名 public class Program { public delegate void myC1(string myStr);

定义:

delegate void myC1(string mystring); 
方法:

public static void myProc1 (myC1 method, string mystring);

如何在myProc1中调用Myc1委托?

委托相当于C/C++中的函数指针。您必须指定要调用的方法,并且它应该尊重委托签名

  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                     
        }    
    }


委托是一种类型,它表示对具有特定参数列表和返回类型的方法的引用。实例化委托时,可以将其实例与具有兼容签名和返回类型的任何方法相关联。您可以通过委托实例调用(或调用)该方法。

需要回答以下问题之一。(*方法)(mystring);b。method.mystring;c。(*方法):mystring;d。这个->(*方法)(mystring);e。方法(mystring);