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<bool, int> discount)
{
int magicDiscount = discount(false);
int magicDiscount2 = discount(true);
Console.WriteLine(magicDiscount);
Console.WriteLine(magicDiscount2);
Console.ReadLine();
}
}
}
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<bool, int> 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!"); };