博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
委托转换,委托重载
阅读量:6269 次
发布时间:2019-06-22

本文共 2117 字,大约阅读时间需要 7 分钟。

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1

{
    delegate void MyAction();
    delegate void MyAction<T>(T obj);
    delegate void MyAction<T1, T2>(T1 who, T2 say);

    delegate string MyFunc();

    delegate T MyFunc<T>();
    delegate TResult MyFunc<T, TResult>(T give);
    delegate TResult MyFunc<T1, T2, TResult>(T1 give, T2 n);
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Action------------------");
            Action a = () => { Console.WriteLine("hello"); };
            a.Invoke();

            Action<string> b = x => { Console.WriteLine(x); };

            b("hi");

            Action<string, string> c = (x, y) => Console.WriteLine(x + " say " + y);

            c("Mr.Du", "Hello");

            Console.WriteLine("MyAction------------------");

            MyAction a1 = a.Method.CreateDelegate(typeof(MyAction)) as MyAction;

            a1();

            MyAction<string> b1 = b.Method.CreateDelegate(typeof(MyAction<string>)) as MyAction<string>;

            b1("hi1");

            MyAction<string, string> c1 = c.Method.CreateDelegate(typeof(MyAction<string, string>)) as MyAction<string, string>;

            c1("Mr.Du", "Hello1");

            Console.WriteLine("Func------------------");

            Func<string> f1 = () => { return "f1"; };

            Console.WriteLine(f1());

            Func<string, string> f2 = x => { return x + x; };

            string give = "a";
            Console.WriteLine(f2(give) + " get of " + give);

            Func<string, int, string> f3 = (x, n) =>

            {
                for (int i = 0; i < n; i++)
                {
                    x += x;
                }
                return x;
            };
            Console.WriteLine(f3(give, 3) + " get of " + give);

            Console.WriteLine("MyFunc------------------");

            MyFunc mf1 = f1.Method.CreateDelegate(typeof(MyFunc)) as MyFunc;

            Console.WriteLine(mf1());

            MyFunc<string> mf2 = f1.Method.CreateDelegate(typeof(MyFunc<string>)) as MyFunc<string>;

            Console.WriteLine(mf2());

            MyFunc<string, string> mf3 = f2.Method.CreateDelegate(typeof(MyFunc<string, string>)) as MyFunc<string, string>;

            Console.WriteLine(mf3(give) + " get of " + give);

            MyFunc<string, int, string> mf4 = f3.Method.CreateDelegate(typeof(MyFunc<string, int, string>)) as MyFunc<string, int, string>;

            Console.WriteLine(mf4(give,3) + " get of " + give);
        }
    }
}

转载于:https://www.cnblogs.com/cppss/p/3950930.html

你可能感兴趣的文章
Spring + Hibernate 框架
查看>>
添加浏览器的用户样式表
查看>>
LigerUI学习笔记之布局篇 layout
查看>>
LeetCode题解(二)
查看>>
Mybatis通用Mapper
查看>>
文件磁盘命令(就该这么学6章内容)
查看>>
2016-207-19 随笔
查看>>
java的double类型如何精确到一位小数?
查看>>
看看国外的javascript题目,你能全部做对吗?
查看>>
ffmpeg 如何选择具有相同AVCodecID的编解码器 (AVCodec)
查看>>
真正解决 Windows 中 Chromium “缺少 Google API 密钥” 的问题
查看>>
Spring 之 AOP
查看>>
软件项目管理|期末复习(二)
查看>>
直接调用VS.net2005中的配置界面
查看>>
程序员的自我修养五Windows PE/COFF
查看>>
关于字符集,编码格式,大小端的简单总结
查看>>
js string 转 int Number()
查看>>
课堂练习:ex 4-20
查看>>
20155328 2016-2017-2 《Java程序设计》 第8周学习总结
查看>>
python操作redis--string
查看>>