• .NET的委托和匿名函数应用一例


    闲话休提,大家都是成年人,直接上代码。有代码有J8:

    delegate string dlgGetTotal();
    void TongJi()
    {
    dlgGetTotal getTotalInt = () => {//统计整数
        int d = 0;
        foreach (KeyValuePair<string, string> kvp in dict)
        {
            d += Convert.ToInt32(kvp.Value);
        }
        return d.ToString();
    };
    dlgGetTotal getTotalDecimal = () => {//统计浮点数
        decimal d = 0.00m;
        foreach (KeyValuePair<string, string> kvp in dict)
        {
            d += Convert.ToDecimal(kvp.Value);
        }
        return d.ToString();
    };
            //标题及左标题
            string stotal = "";
            switch (tjcontent)
            {
                case "islandcount":
                    statTitle = @"楼盘数量";
                    statUnit = "个";
                    stotal = getTotalInt();
                    break;
                case "area":
                    statTitle = @"面积";
                    statUnit = "平方米";
                    stotal = getTotalDecimal();
                    break;
                case "huji":
                    statTitle = @"户籍人口";
                    statUnit = "人";
                    stotal = getTotalInt();
                    break;
                case "changzhu":
                    statTitle = @"常住人口";
                    statUnit = "人";
                    stotal = getTotalInt();
                    break;
            }
            statTitle += String.Format("统计(合计:{0} {1})",stotal,statUnit);
            statUnit = "单位:" + statUnit;
    }

    以上是一段用于统计数字的代码。数字有整数,也有带小数的浮点数,因此要区别对待。故而用了委托、匿名函数、lambda表达式。

    版权声明:本文为博主原屙文章,喜欢你就担走。

  • 相关阅读:
    CF601B Solution
    CF847F Solution
    CF877D Solution
    CF1472F Solution
    CF1472G Solution
    CF1355E Solution
    CF912D Solution
    CF1167F Solution
    shell脚本自动备份MySQL数据库
    centos7 crontab笔记
  • 原文地址:https://www.cnblogs.com/leftfist/p/4764233.html
Copyright © 2020-2023  润新知