子曾经曰过

  博客园  :: 首页  ::  ::  ::  :: 管理

有这么一句话,把方法传递给其他方法,需要使用委托。什么意思呢?

委托是方法的类型安全的引用。

参数倒知道,把参数传递给其他方法,这个方法传递给其他方法,是怎么回事呢?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace DelegateStudyWell
{
class Program
{
public delegate int TakesAWhileDelegate(int data, int ms);
static void Main(string[] args)
{
//投票技术
//TakesAWhileDelegate dl = TakesAWhile;
//IAsyncResult ar = dl.BeginInvoke(1, 3000, null, null);

//while (!ar.IsCompleted)
//{
// Console.WriteLine(".");
// System.Threading.Thread.Sleep(50);
//}
//int result = dl.EndInvoke(ar); //返回函数调用后的返回值
//Console.WriteLine("result:{0}", result);
//异步回调
TakesAWhileDelegate dl = TakesAWhile;
dl.BeginInvoke(
1, 3000, TakesAWhileCompleted, dl);
for (int i = 0; i < 100; i++)
{
Console.Write(
".");
System.Threading.Thread.Sleep(
50);
}





Console.ReadLine();

}
static int TakesAWhile(int data, int ms)
{
Console.WriteLine(
"begin");
System.Threading.Thread.Sleep(ms);
Console.WriteLine(
"completed");
return ++data;
}
static void TakesAWhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
TakesAWhileDelegate dl
= ar.AsyncState as TakesAWhileDelegate;
int result = dl.EndInvoke(ar);
Console.WriteLine(
"result:{0}", result);
}


}

}
posted on 2011-02-24 11:34  人的本质是什么?  阅读(649)  评论(0编辑  收藏  举报