委托
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。
委托是方法的抽象,它存储的就是一系列具有相同签名和返回类型的方法的地址。调用委托的时候,委托包含的所有方法将被执行。 委托是类型,就好像类是类型一样。与类一样,委托类型必须在被用来创建变量以及类型对象之前声明。 委托的声明原型是
delegate <函数返回类型> <委托名> (<函数参数>)
定义一个people类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 委托
{
class People
{
public string Talk()
{
return "你好,我是一只咪";
}
public int Add(int a,int b)
{
return a + b;
}
}
}
Program类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 委托
{
delegate string DeleTalk();
delegate int DeleAdd(int a, int b);
class Program
{
static void Main(string[] args)
{
Console.WriteLine("哈哈哈哈哈哈哈!");
People p = new People();
//p.Talk();
//委托:可以理解为一种数据类型,这种数据类型是直接赋值一个方法
//DeleTalk a = SetName;
SetName(p.Talk);
SetNum(p.Add);
Console.ReadLine();
}
static void SetName(DeleTalk talk)
{
Console.WriteLine(talk());
}
static void SetNum(DeleAdd add)
{
Console.WriteLine("两数之和为:"+add(5,6));
}
}
}
Action
Action是无返回值的泛型委托。
Action 表示无参,无返回值的委托
Action<int,string> 表示有传入参数int,string无返回值的委托
Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托
Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托
Action至少0个参数,至多16个参数,无返回值。
Func
Func是有返回值的泛型委托
Func 表示无参,返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<T1,T2,T3,int> 表示传入参数为T1,T2,T3(泛型)返回值为int的委托
Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void
反射
反射的作用:
1、可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型
2、应用程序需要在运行时从某个特定的程序集中载入一个特定的类型,以便实现某个任务时可以用到反射。
3、反射主要应用与类库,这些类库需要知道一个类型的定义,以便提供更多的功能。
特性
特性(attribute)是被指定给某一声明的一则附加的声明性信息。
C#多播委托
每个委托都只包含一个方法调用,调用委托的次数与调用方法的次数相同。如果调用多个方法,就需要多次显示调用这个委托。当然委托也可以包含多个方法,这种委托称为多播委托。
当调用多播委托时,它连续调用每个方法。在调用过程中,委托必须为同类型,返回类型一般为void,这样才能将委托的单个实例合并为一个多播委托。如果委托具有返回值和/或输出参数,它将返回最后调用的方法的返回值和参数。
多播委托是指在一个委托中注册多个方法,在注册方法是可以在委托中使用加号运算符或者减号运算符来实现添加或撤销方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 多播委托
{
class Program
{
//多播委托:一个委托中注册多个方法
//定义购买商品的委托
public delegate void OrderDelegate();
static void Main(string[] args)
{
//实例化委托
OrderDelegate orderDelegate = new OrderDelegate(BuyFood);
//向委托中注册方法
orderDelegate += BuyFlower;
orderDelegate += BuyCake;
//调用委托
orderDelegate();
orderDelegate -= BuyFlower;
orderDelegate();
Console.ReadLine();
}
public static void BuyFood()
{
Console.WriteLine("购买快餐!");
}
public static void BuyCake()
{
Console.WriteLine("购买蛋糕!");
}
public static void BuyFlower()
{
Console.WriteLine("购买鲜花!");
}
}
}
匿名委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 匿名委托
{
class Program
{
static void Main(string[] args)
{
//Action Say = delegate { Console.WriteLine("你好啊!"); };
Action Say = () => Console.WriteLine("你好啊!");
Say();
//Action<string> Speak = delegate (string str) { Console.WriteLine("你好!" + str); };
Action<string> Speak = (string str) => Console.WriteLine("你好!" + str);
Speak("一只咪");
//Func<int, int, int> Add = delegate (int i, int j) { return i + j; };
Func<int, int, int> Add = (int i, int j) => i + j;
Console.WriteLine( Add(5, 6));
Console.ReadLine();
}
}
}
事件
事件是一种引用类型,实际上是一种特殊的委托,通常,每一个事件的发生都会产生发送方和接收方,发送方是指引用事件的对象,接收方则是指获取,处理事件,事件要与委托一起使用,事件定义的语法形式如下:
访问修饰符 event 委托名 事件名;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 事件
{
class Program
{
//定义委托
public delegate void SayDelegate();
//定义事件
public event SayDelegate SayEvent;
//定义委托中调用的方法
public void SayHello()
{
Console.WriteLine("Hello Event!");
}
//创建触发事件的方法
public void SayEventTrigger()
{
//触发事件,必须与事件是同名的方法
SayEvent();
}
static void Main(string[] args)
{
//创建Program类的实例
Program program = new Program();
//实例化事件,使用委托指向处理方法
program.SayEvent = new SayDelegate(program.SayHello);
//调用触发事件的方法
program.SayEventTrigger();
Console.ReadLine();
}
}
}