Pages

Saturday, September 18, 2010

Anonymous Methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Net3p5Features.Expression
{
public class AnonymouseMethodTest : IProgramTest
{
public string Name
{
get { return "Anonymouse Sample"; }
}

public string Description
{
get { return Name; }
}

public void Run()
{
var car = new Car
{
Brand = "BMW",
Model = "92",
OnRunning =delegate(object sender,EventArgs e)
{
Console.WriteLine("sender :{0} called from Anynomouse method", sender);
var s = "Hello world";
}
};
car.Run();

var car2 = new Car
{
Brand = "BMW",
Model = "92",
OnRan =
(sender, e) =>
{
Console.WriteLine("sender :{0} called from Anynomouse method", sender);
var s = "Hello world";
}
};
car2.Run();
}
}

public class Car
{
public string Brand { get; set; }
public string Model { get; set; }
public EventHandler OnRunning;
public EventHandler OnRan;

public void Run()
{
if (OnRunning != null) OnRunning(this, EventArgs.Empty);
Console.WriteLine("Model:{0}, Brand:{1}", Model, Brand);
if (OnRan != null) OnRan(this, EventArgs.Empty);

}

}


}

No comments:

Post a Comment