委托的学习以及使用

每个人都委托都有不同的见解,因为看问题的角度不同。个人认为,可以从以下2点来理解:
(1) 从数据结构来讲,委托是和类一样是一种用户自定义类型。
(2) 从设计模式来讲,委托(类)提供了方法(对象)的抽象。
既然委托是一种类型,那么它存储的是什么数据?
我们知道,委托是方法的抽象,它存储的就是一系列具有相同签名和返回回类型的方法的地址。调用委托的时候,委托包含的所有方法将被执行。
2. 委托类型的定义
委托是类型,就好像类是类型一样。与类一样,委托类型必须在被用来创建变量以及类型对象之前声明。
//接下来我们就简单的写几个例子
class Program
{
delegate void Delegate1();//定义一个委托
static void Main(string[] args)
{

            **//声明委托对象**
            Delegate1 dele1;
            //无参无返回值类型的
            //方法一:
            Student stu = new Student();
             **//给委托绑定方法,也就是让委托持有方法**
            dele1 = stu.ShouName;//让这个委托持有学生类的显示名字的方法
            dele1 += stu.ShouAge;//给委托绑定又绑定了一个方法
            dele1.Invoke();//方法调用
           //方法二
           Task1(stu.ShouAge);

        }
   static void Task1(Delegate1 dele)
    {
        dele.Invoke();
    }
    class Student
    {

        public void ShouName()
        {

            Console.WriteLine("张三");
        }

        public void ShouAge()
        {

            Console.WriteLine(18);
        }
    }

}

//有参数有返回值类型的
方法一
class Program
{
delegate int Delegate1(int a,int b);//定义委托
static void Main(string[] args)
{
声明一个委托对象
//Delegate1 delegate1;
给委托绑定方法,让委托持有方法
delegate1 = Tools.Sub;
int result = delegate1(3,5);
Console.WriteLine(result);

}
class Tools
{
    public static int Add(int a,int b)
    {
        return a + b;
    }
    public static int Sub(int a, int b)
    {
        return a - b;
    }
}
方法二
   class Program
{
    delegate int Delegate1(int a,int b);//定义委托
    static void Main(string[] args)
    {
        声明一个委托对象
        //Delegate1 delegate1;
        给委托绑定方法,让委托持有方法
     int result= MyInvoke(Tools.Add,5,5)
         Console.WriteLine(result);
}
     static int MyInvoke(Delegate1 delegate1,int a,int b)
    {
        int result = delegate1.Invoke(a,b);
        return result;
    }

class Tools
{
    public static int Add(int a,int b)
    {
        return a + b;
    }
    public static int Sub(int a, int b)
    {
        return a - b;
    }
}

//泛型委托
//1.我们自己定义一个
// delegate void MyDelegate();
//写一个学生类并添加方法
//class Student
//{
// public void Tast()
// {
// Console.WriteLine(12356);
// }
//}
//主程序调用
//MyDelegate dele =
// new MyDelegate(new Student().Tast
// );
//dele.Invoke();
//2. 我们也可以用Action调用,这时候就不用定义委托了
//直接用学生类
//class Student
//{
// public void Tast()
// {
// Console.WriteLine(12356);
// }
//}
//在主程序调用
// Student s = new Student();
//开始委托
//Action action = s.Tast;//如果Tast没有参数 Action不加<>,如果Tast有参数,则需加<>例如Tast(int num),则 Action;
//3.用Func委托 func类型必须是有参数和返回值类型的
例如Func< int,string,int> <>里最后的参数是方法返回的类型,前几个对应方法参数类型
class Student
{
public int Tast(int num)
{
return num;
}
}
Func< int,int > func = s.Tast;
// int num= func(5);//这么调用,然后输出
//Console.WriteLine(num)
//或者直接输出
Console.WriteLine(func.Invoke(5));

//用lambda表达式或者匿名方法
//public delegate void MyDelegate(string name);
//把委托定义到类的外部
public class PersonManager
{
//事件定义到类的内部,用来封装,定义事件的关键字 event,用委托来声明事件
public event Action MyEvent**;
public event Action MyEventint;
//触发事件的方法
public void Exec( string name,int age = 18)
{
if (age!=18)
{
MyEvent.Invoke(name);
MyEventint.Invoke(age);
}else
{
MyEvent.Invoke(name);
}

        }


    }

static void Main(string[] args)
{
PersonManager personManager = new PersonManager();

//使用匿名方法绑定事件的处理,订阅事件
personManager.MyEvent += delegate (string name) {
Console.WriteLine(“My name is ” + name);
};
//使用lambda表达式
personManager.MyEventint += (int age) =>
{
Console.WriteLine(“我的年龄是:”+age);
};

 //执行事件,若Age是18 执行名字和年龄
 //若不是18 只有名字
 personManager.Exec("张三",20);
    }
}
//使用匿名函数与fUNC

//匿名函数 a 是 第一个double 的形参,condition 是第二个bool类型的形参
Func< double, bool, double> func = (a, condition) =>
{
if (condition)
{
return a * 1.5;
}
else
{
return a * 2;
}
};
double result = func(1000, false);
Console.WriteLine(“Result is : ” + result);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值