标签:
delegate string DelegateMethod(int a, int b);//定义委托
class Program
{
static void Main(string[] args)
{
DelegateMethod dm = new DelegateMethod(Method);//或:DelegateMethod dm = Method
Console.WriteLine(dm(10, 20));//执行委托
Console.Read();
}
private static string Method(int a, int b)
{
return "相加结果:" + (a + b);
}
}
delegate string DelegateMethod(int a, int b);//定义委托
class Program
{
static void Main(string[] args)
{
DelegateMethod dm = Method;
Console.WriteLine(dm.Invoke(10, 20));//执行委托
Console.Read();
}
private static string Method(int a, int b)
{
return "相加结果:" + (a + b);
}
}
delegate void DelegateMethod(int a, int b);//定义委托
class Program
{
static void Main(string[] args)
{
DelegateMethod dm = Method;
for (int i = 0; i < 10; i++)
{
dm.BeginInvoke(i, 20, null, null);//异步调用委托
}
Console.Read();
}
private static void Method(int a, int b)
{
Console.WriteLine("相加结果:" + (a + b));//执行委托
}
}
delegate void DelegateMethod(int a, int b);//定义委托
class Program
{
static void Main(string[] args)
{
DelegateMethod dm = Method;
dm.BeginInvoke(10, 20, MethodCompleted, null);//异步调用委托
Console.Read();
}
//异步委托
private static void Method(int a, int b)
{
Console.WriteLine("相加结果:" + (a + b));
Thread.Sleep(3000);
}
//回调函数
private static void MethodCompleted(IAsyncResult ar)
{
Console.WriteLine("休眠结束!");
}
}
delegate string DelegateMethod(int a, int b);//定义委托
class Program
{
static void Main(string[] args)
{
DelegateMethod dm = Method;
IAsyncResult ar = dm.BeginInvoke(10, 20, null, null);//异步调用委托
string result = dm.EndInvoke(ar);//等待委托异步调用结束
Console.WriteLine(result);//输出返回值
Console.Read();
}
//异步委托
private static string Method(int a, int b)
{
Thread.Sleep(3000);
return "相加结果:" + (a + b);
}
}
class Program
{
static void Main(string[] args)
{
Action<int,int> dm = Method;//委托实例
dm(10,20);//调用委托
Console.Read();
}
private static void Method(int a, int b)
{
Console.WriteLine("相加结果:" + (a + b));
}
}
class Program
{
static void Main(string[] args)
{
Func<int, int, string> dm = Method;//委托实例
string result = dm(10, 20);//调用委托
Console.WriteLine(result);//输出返回值
Console.Read();
}
private static string Method(int a, int b)
{
return "相加结果:" + (a + b);
}
}
class Program
{
static void Main(string[] args)
{
Predicate<int> dm = Method;//委托实例
if (dm(12))//调用委托
{
Console.WriteLine("偶数");
}
else
{
Console.WriteLine("奇数");
}
Console.Read();
}
private static bool Method(int a)
{
if(a%2==0)
{
return true;
}
return false;
}
}
class Program
{
static void Main(string[] args)
{
Action<int> dm = null; //委托实例
for (int i = 0; i < 20; i++)
{
if (i % 2 == 0)
{
dm += Method1;
}
else
{
dm += Method2;
}
}
dm(123456);//调用多播委托
Console.Read();
}
private static void Method1(int a)
{
Console.WriteLine("委托1:" + a);
}
private static void Method2(int a)
{
Console.WriteLine("委托2:" + a);
}
}
class Program
{
static void Main(string[] args)
{
Action<int> dm = delegate(int a)
{
Console.WriteLine(a.ToString());
};//匿名方法
dm(123456);//调用委托指向的匿名方法
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
//标准的Lambda格式
Func<int, int, string> sum0 = (int a, int b) => { return "和是:" + (a + b); };
Console.WriteLine(sum0(10, 20));
//简写格式
Func<int, int, string> sum1 = (a, b) => "和是:" + (a + b);
Console.WriteLine(sum1(30, 40));
Action<int, int> sum2 = (a, b) => Console.WriteLine("和是:" + (a + b));
sum2(50, 60);
Console.Read();
}
}
Action<int, int> sum = (a, b) => { int i = a = +b; Console.Write(i); };
Action<int, int> sum = (a, b) => Console.Write(a + b);
Func<int, int, string> sum0 = (a, b) => { return "和是:" + (a + b); };
Func<int, int, string> sum1 = (a, b) => "和是:" + (a + b);
List<int> list = new List<int>();
list.RemoveAll(i => i < 100);//i会被猜测为int
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4315643.html