simple anonymous methods

using System;

namespace test
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//new ShoppingCart().Process(new Func<bool, int>(delegate(bool x) { return x ? 10 : 5; })); 

            
// can also be
            new ShoppingCart().Process(delegate(bool x){ return x ? 10 : 5; });
        } 
    }

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


// creates an anonymous comparer  

custs.Sort(delegate(Customer c1, Customer c2){return Comparer<int>.Default.Compare(c1.ID, c2.ID); });

 

// creates an anonymous event handler

button1.Click += delegate(object o, EventArgs e) { MessageBox.Show("Click!"); };

 

posted on 2009-12-22 15:52  Jack Han  阅读(113)  评论(0编辑  收藏  举报

导航