simple lambda expressions

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();
        }
    }
}


posted on 2009-12-22 16:23  Jack Han  阅读(188)  评论(0编辑  收藏  举报

导航