Monday, March 23, 2009

Codes example for why use Lambda

Consider this example:
 string person = people.Find(person => person.Contains("Joe"));

versus
 public string FindPerson(string nameContains, List<string> persons)
 
{
     
foreach (string person in persons)
         
if (person.Contains(nameContains))
             
return person;
     
return null;
 
}

1 comment:

  1. Example code for anonomous function
    Code:
    button1.Click += new EventHandler(button1_Click);
    void button1_Click (object sender, EventArgs e)
    {
    textbox1.Text = DateTime.Now.ToShortDateString();
    }

    Lambda Code:
    button1.Click += (s, e) => { textbox1.Text = DateTime.Now.TosShortDateString(); } ;

    ReplyDelete