public delegate double Delegate_Prod(int a,int b); class Class1 { static double fn_Prodvalues(int val1,int val2) { return val1*val2; } static void Main(string[] args) { //Creating the Delegate Instance Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues); Console.Write("Please Enter Values"); int v1 = Int32.Parse(Console.ReadLine()); int v2 = Int32.Parse(Console.ReadLine()); //use a delegate for processing double res = delObj(v1,v2); Console.WriteLine ("Result :"+res); Console.ReadLine(); } }
namespace ConsoleApplication { //定义代理 delegate String Mydelegate(); class temp { static void Main(String[] args) { //实例化代理 Mydelegate Md = new Mydelegate(AsyncMethod); //实例一个回调代理 AsyncCallback callback = new AsyncCallback(callbackMethod); //开始执行异步方法 Md.BeginInvoke(callback, Md); Console.ReadLine(); } //异步调用的方法 static String AsyncMethod() { Console.WriteLine("异步方法正执行"); String str = "异步调用已结束"; return str; } //回调方法 static void callbackMethod(IAsyncResult Ias) { Mydelegate Md = (Mydelegate)Ias.AsyncState; String str = Md.EndInvoke(Ias); Console.WriteLine(str); } } }
原文:http://www.cnblogs.com/ilookbo/p/4938222.html