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

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

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

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


}

}

相关文章:

  • 2021-07-08
  • 2022-02-01
  • 2022-01-10
  • 2021-11-21
  • 2018-12-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2021-05-16
  • 2022-12-23
  • 2021-05-22
相关资源
相似解决方案