simple lambda expressions

2019/7/24 3:40:31 人评论 次浏览 分类:学习教程

原文链接:http://www.cnblogs.com/hanjack/archive/2009/12/22/1629879.html
using System;

namespace test
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Func
<boolint> del = x => x ? 10 : 5;
            
new ShoppingCart().Process(del);

            
// can also be
            
//new ShoppingCart().Process(x => x ? 10 : 5); 

            
// can also be
            
//new ShoppingCart().Process(
            
//x =>
            
//{
            
//    int discount = 0;
            
//    if( x )
            
//    {
            
//        discount = 20;
            
//    }
            
//    else if (DateTime.Now.Hour < 12)
            
//    {
            
//        discount = 5;
            
//    }
            
//    else if (DateTime.Now.Hour < 20)
            
//    {
            
//        discount = 10;
            
//    }
            
//    else
            
//    {
            
//        discount = 15;
            
//    }
            
//    return discount;
            
//}); 
        } 
    }

    
class ShoppingCart
    {
        
public void Process(Func<boolint> discount)
        {
            
int magicDiscount = discount(false);
            
int magicDiscount2 = discount(true);
            Console.WriteLine(magicDiscount);
            Console.WriteLine(magicDiscount2);
            Console.ReadLine();
        }
    }
}


转载于:https://www.cnblogs.com/hanjack/archive/2009/12/22/1629879.html

相关资讯

    暂无相关的资讯...

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->