.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表达式。

你可能感兴趣的:(匿名函数,lambda,委托)