zoukankan      html  css  js  c++  java
  • Func<T, TResult> 委托的由来和调用和好处(为了高大上,为了白富美)

    Func<T, TResult>是系统的内置委托的中最常用的一个。特点就是必须有一个返回值。(func委托有多个重载,所有重载的最后一个参数就是返回值的类型,前面的是参数类型)。注:没有返回值的系统内置委托是Action<T>

    Func委托的作用就是当我们需要传入n参数并有返回值时,我们不用再去定义一个委托,直接调用func即可。

    例如: 当我们输入一个字符串,然后把这个字符串转换为大写并返回。

    我们最原始的做法是:先写一个方法

            public string toUpperStr(string SourceStr)
            {
                return SourceStr.ToUpper();
            }

    然后调用

            public ActionResult Index()
            {
                string sourceStr = "SuGuoTong";
                ViewBag.Result = ToUpperStr(sourceStr);
                return View();
            }

    但如果还需要把字符串改成小写,还得在增加一个方法,为了方便,所以我们引进委托。

    更重要的是上面的代码一看就是屌丝代码,为了高大上,为了白富美,我们在行动。

            delegate string ConvertMethod(string str);//传入一个参数和返回一个返回值的委托
            public ActionResult Index()
            {
                string sourceStr = "SuGuoTong";
                ConvertMethod convert = ToUpperStr;
                ViewBag.Result = convert(sourceStr);
                return View();
            }
            public string ToUpperStr(string SourceStr)
            {
                return SourceStr.ToUpper();
            }

    把上面我们自己声明的委托改为系统内置的委托Func<T, TResult>

            public ActionResult Index()
            {
                string sourceStr = "SuGuoTong";
                Func<string, string> convert = ToUpperStr; //Func<string,string>第一个string是参数类型,第二个string是返回值类型
                ViewBag.Result = convert(sourceStr);
                return View();
            }
            public string ToUpperStr(string SourceStr)
            {
                return SourceStr.ToUpper();
            }
    //变白了,想不想变富

    为了变富,我们把下面的方法也去掉,这时候就要用到匿名方法

            public ActionResult Index()
            {
                string sourceStr = "SuGuoTong";
                Func<string, string> convert = delegate(string s) { return s.ToUpper(); };
                ViewBag.Result = convert(sourceStr);
                return View();
            }
    //是不是又白又富了,想不想变美

    为了变美,我们引进lambda(亚麻带)表达式

            public ActionResult Index()
            {
                string sourceStr = "SuGuoTong";
                Func<string, string> convert = o=>o.ToUpper();
                ViewBag.Result = convert(sourceStr);
                return View();
            }
    //成了白富美,就可以进军岛国了

    完!

    调用系统方法需传入func的例子:

                string[] strAry = { "gs","gsd","htrh"};
                var resl = strAry.Select<string, string>(o => o.ToUpper());

    附:匿名方法

    关于调用系统的方法,我们需要传匿名方法的例子很多:

    比如Array.ConvertAll方法把一个string[]转换成int[]

    这就是需要我们传一个匿名方法(传入一个string类型参数,返回一个int类型的匿名方法)

    intArr = Array.ConvertAll<string, int>(str, delegate(string s) { return int.Parse(s) * 5; });
    或直接用lambda代替
    intArr = Array.ConvertAll<string, int>(str, o => int.Parse(o) * 5);
  • 相关阅读:
    es6中新增的字符串函数
    模板字符串
    jsp注释
    EL表达式(自己看的)
    在禁用cookie时操作Session
    urlEncoder和urlDecoder的作用和使用
    jsp中写java代码的方法
    通过类加载器在WEB应用中获取资源文件路径
    统计在线人数
    Mysql数据库操作简单版
  • 原文地址:https://www.cnblogs.com/shinima/p/4000358.html
Copyright © 2011-2022 走看看