Pages

Sunday, September 19, 2010

Anonymous Methods 2

using System;


namespace DeligateTests.GenericDelegate
{

class GenericDelegateTest2 : IProgramTest
{
public string Name
{
get { return "Test Func method"; }
}

public string Description
{
get { return Name; }
}

public void Run()
{
var cus = new Customer {ID = 1, Name = "Jan, Joseph", BirthDate = DateTime.Parse("18/12/1971")};
Console.WriteLine(cus.ToString(c => c.Name + ", " + c.ID.ToString()));
Console.WriteLine(cus.ToString(c => c.BirthDate.ToShortDateString()));
// old way
Console.WriteLine(cus.ToString(delegate(Customer c) { return c.Name;}));


}
}

public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public string ToString(Func<Customer,string> predicate)
{
return predicate(this);
}
}
}

No comments:

Post a Comment