使用委托来做一些事情,大致思路是:

1、定义声明一个委托,规定输入参数和输出类型。
2、写几个符合委托定义的方法。
3、把方法列表赋值给委托
4、执行委托

    internal delegate int MyDelegate();

    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate d = ReturnOne;
            d += ReturnTwo;
            foreach (int i in GetAllReturnVals(d))
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }

        static IEnumerable<int> GetAllReturnVals(MyDelegate myDelegate)
        {
            foreach (MyDelegate del in myDelegate.GetInvocationList())
            {
                yield return del();
            }
        }

        static int ReturnOne()
        {
            return 1;
        }

        static int ReturnTwo()
        {
            return 2;
        }
    }

以上,委托的返回类型是int,如果让返回类型是泛型呢?只要让以上的GetAllReturnVals方法针对泛型就可以了。

   internal delegate T MyDelegate<T>();

    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate<int> d = ReturnOne;
            d += ReturnTwo;
            foreach (int i in GetAllReturnVals(d))
            {
                Console.WriteLine(i);
            }

            MyDelegate<string> d1 = ReturnA;
            d1 += ReturnB;
            foreach (string s in GetAllReturnVals(d1))
            {
                Console.WriteLine(s);
            }
            Console.ReadKey();
        }

        //返回所有委托方法的执行结果
        static IEnumerable<T> GetAllReturnVals<T>(MyDelegate<T> myDelegate)
        {
            foreach (MyDelegate<T> del in myDelegate.GetInvocationList())
            {
                yield return del();
            }
        }

        static int ReturnOne()
        {
            return 1;
        }

        static int ReturnTwo()
        {
            return 2;
        }

        static string ReturnA()
        {
            return "A";
        }

        static string ReturnB()
        {
            return "B";
        }
    }


不过,.NET还为我们准备了一个更"懒 "的方法,那就是针对泛型的委托Func<T>,这下,连委托都不要事先声明了。Func<T>的<>中的泛型列表的最后一个是返回类型,其它的都是输入类型,Func<T>多达十几个重载。

   class Program
    {
        static void Main(string[] args)
        {
            Func<int> d = ReturnOne;
            d += ReturnTwo;
            foreach (int i in GetAllReturnVals(d))
            {
                Console.WriteLine(i);
            }

            Func<string> d1 = ReturnA;
            d1 += ReturnB;
            foreach (string s in GetAllReturnVals(d1))
            {
                Console.WriteLine(s);
            }
            Console.ReadKey();
        }

        //返回所有委托方法的执行结果
        static IEnumerable<T> GetAllReturnVals<T>(Func<T> myDelegate)
        {
            foreach (Func<T> del in myDelegate.GetInvocationList())
            {
                yield return del();
            }
        }

        static int ReturnOne()
        {
            return 1;
        }

        static int ReturnTwo()
        {
            return 2;
        }

        static string ReturnA()
        {
            return "A";
        }

        static string ReturnB()
        {
            return "B";
        }
    }


以上泛型委托Func<T>同时有输入参数和返回类型,如果返回类型为void,那就可以使用Action<T>了,当然Action<T>也有多达十几个重载。

   class Program
    {
        static void Main(string[] args)
        {

            Action<int> a1 = ReturnInt;
            a1(1);

            Action<string> a2 = ReturnStr;
            a2("hello");

            Console.ReadKey();
        }

        static void ReturnStr(string s)
        {
            Console.WriteLine(s);
        }

        static void ReturnInt(int a)
        {
            Console.WriteLine(a);
        }


    }


总结:Func<T>和Action<T>是泛型委托的"语法糖",如果返回类型为void,那就使用Action<T>,否则使用Func<T>。

委托, 泛型委托,Func<T>和Action<T>的更多相关文章

  1. 【C#复习总结】细说泛型委托

    1 前言 本系列会将[委托] [匿名方法][Lambda表达式] [泛型委托] [表达式树] [事件]等基础知识总结一下.(本人小白一枚,有错误的地方希望大佬指正) 系类1:细说委托 系类2:细说匿名 ...

  2. C#编程(三十)----------泛型结构,泛型方法,泛型委托

    泛型结构 泛型结构和泛型类几乎是一直的,只是泛型结构没有继承的特性..NET平台提供的一个泛型结构是(可空类型)Nullablle<T>.可空类型的引入,主要是为了解决数据库语言中的数字与 ...

  3. Func<T>与Action<T>委托泛型介绍:转

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

  4. Func<T>与Action<T>委托泛型介绍

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

  5. 泛型委托 Predicate/Func/Action

    Predicate 泛型委托  表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素.看看下面它的定义: // Summar ...

  6. C#的泛型委托Predicate/Func/Action(转)

    Predicate 泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素. 类型参数介绍:    T: 要比较的对 ...

  7. .NET中的Action及Func泛型委托

    委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调机制的实现基 ...

  8. (C#) Action, Func, Predicate 等泛型委托

    (转载网络文章) (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public del ...

  9. 使用.NET中的Action及Func泛型委托

          委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调 ...

随机推荐

  1. casperjs get开头的几个dom操作使用

    getCurrentUrl() Signature: getCurrentUrl() Retrieves current page URL. Note that the url will be url ...

  2. IntelliJ IDEA 中安装junit插件

    1.在Intellij IDEA 中安装了 Junit,TestNG插件,首先检查一下intellij IDEA 是否在安装时默认安装了这两个插件,检查方法 打开 settings -->Plu ...

  3. express中间件代理实现跨域

    前端代码 var xhr = new XMLHttpRequest(); xhr.open('post', 'http://localhost:3000', true); xhr.onreadysta ...

  4. VS2010上连接SQLite数据库

    VS2010连接SQLite数据库 Visual studio 2010及以上版本,连接SQLite数据库 1.在Sqlite开发站点下载SQLite的.exe安装包 Ctrl+F搜索这条语句:Thi ...

  5. java采用zip方式实现String的压缩和解压缩CompressStringUtil类

    CompressStringUtil类:不多说,直接贴代码: /** * 压缩 * * @param paramString * @return */ public static final byte ...

  6. django之class Meta

    通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...

  7. TinyHttpd代码解析

    十一假期,闲来无事.看了几个C语言开源代码.http://www.cnblogs.com/TinyHttpd 这里本来想解析一下TinyHttpd的代码,但是在网上一搜,发现前辈们已经做的很好了.这里 ...

  8. python tar.gz格式压缩、解压

    一.压缩 需求描述 现在有一个目录,需要将此目录打包成tar.gz文件.因为有一个Django项目,需要用到此功能! tar.gz 目录结构如下: ./ ├── folder │   ├── .doc ...

  9. Java编程的逻辑 (28) - 剖析包装类 (下)

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  10. Java并发编程、多线程、线程池…

    <实战java高并发程序设计>源码整理https://github.com/petercao/concurrent-programming/blob/master/README.md Ja ...