C#委托代理

发表于2017-10-20
评论0 1.2k浏览

委托代理 delegate

委托是一种数据类型,他可引用一个或多个方法

委托是一种引用方法的类型,一旦委托得到了方法,委托将与该方法拥有一样的行为

委托可以接受方法的类型,需要和方法类型一致

委托方法的使用可以像任何其他方法一样使用,具有参数和返回值

 

访问权限修饰符(internal 或者 public delegate 返回值类型 方法名(参数列表)

一种是没参数,一种有参数且有参数名

 

委托的第一种存在方式就是与类,接口,结构体平级

第二中存在方式在类体的内部和方法,字段,属性平级

 

delege定义在内部是为了用static

委托的数据类型是返回值加参数列表

 

delegate void showMethod();

    class Program{

        //委托的第二种方式就是在类体的内部与方法,字段,属性平级

        //delegate void showMethod();

        static void Main(string[] args) {

            //声明委托实例

            //声明一个委托类型的变量

            showMethod method;

            //给委托赋值

            method = show;

            //使用委托变量

            method();

 

            //showMethod m = show;直接赋值绑定

 

           // showMethod m = new showMethod(show); new的方式来绑定

 

        }

         //void()-->方法的类型(方法签名)

        public static void show() {

            Console.WriteLine("你好");

        }

    }

 

 //匿名委托

    //委托可以接受一个方法类型

    delegate void Method1();

    delegate int Method2(int a,int b);

    class Program {

        static void Main(string[] args) {

            Method1 m1 = show;

            //直接完成委托匿名方式

            //没参数

            Method1 m = delegate () {

                Console.WriteLine("hello world");

            };

            //有参带返回值

            Method2 c = delegate (int a,int b) {

                return a b;

            };

            int sum = c(1,2);

        }

        public static void show() {

 

        }

    }

 

委托组合

namespace VCDelegateCallBack {

    //封装版本的回调

    //下载类

  

    public static class DownloadManager {

        //数据下载方法

        //参数 :下载的数据

        public delegate void DownloadComplete(string result);

        public static DownloadComplete complete;

        public static void DownloadData(String url) {

            //数据下载

            for(int i = 0; i <= 100; i ) {

                Thread.Sleep(100);

                Console.Clear();

                Console.WriteLine("数据下载:" i "%");

            }

            //下载成功

            Console.WriteLine("数据下载成功");

            complete("...嘿嘿嘿嘿.avi");

        }

    }

    public class Person {

        public string name;

        //下载数据

        public void DownloadData(String url) {

            //回调方法

            DownloadManager.complete = DownloadComplete;

            //使用数据下载类来下载数据

            DownloadManager.DownloadData(url);

        }

        public void DownloadComplete(String result) {

            Console.WriteLine(name " " result);

        }

    }

 

 

    class Program {

        static void Main(string[] args) {

            //创建一个人的对象

            Person xiaoming = new Person();

            xiaoming.name = "小明";

            //下载数据

            xiaoming.DownloadData("www.baidu.com");

        }

    }

}

 

//Lambda表达式

            //在方法参数列表和方法体之间添加 => goes to 

            //当前就是Lambda表达式

            Method1 m = () =>{

                Console.WriteLine("我是开始");

            };

            //Lambda表达式

            Method2 m1 = (int x,int y) => {

                return x y;

            };

            //简化1.去掉形参的类型,因为在声明delegate就已经确定了当前形参的数据类型

            Method2 m2 = ( x, y) => {

                return x y;

            };

            //简化 2.若方法体中,只有一个返回值语句,那么大括号,return 都可以不写

            Method2 m3 = (x, y) => x y;

 

            //简化 3.若当前参数列表中只有一个形参,那么形参的小括号也可以省略

            Method3 m4 = x => x * 2;

 

 //回调 lambda表达式版本

    delegate void DownloadCallBack(String data);

    class Program {

        static void Main(string[] args) {

            //下载数据

            DownloadCallBack dlc = data => {

                Console.WriteLine("数据下载完成:" data);

            };

            //数据下载

            for(int i = 0; i <= 100; i ) {

                Thread.Sleep(50);

                Console.Clear();

                Console.WriteLine("数据下载:" i "%");

            }

            //触发回调

            dlc("战狼2.mvk");

        }

    }

 

 

事件只能是在类内做调用

 

 

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签:

0个评论