天天看点

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

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 = "单位:"