Månad: juli 2009

More Lambda syntax, replacing grouping method or anonymous methods

It´s been a long while since I posted something, I have som cool Lambda syntax that I coded today that I would like to share with my readers.

The problem

I have a helper metod that takes a method delegate as parameter, the delegate looks like this.

delegate void MethodDelegate(TParam param)

I´m using this with some other stuff to create thread call´s in my winform applications. It could be calls like CreateUser(User user), the method always takes one parameter. Anyway my Crud classes have some method that uses two parameters like UpdateUser(User user, string key).

Now how do I use this easily with my Delegate, I could write a method that looks like this (I have left out the generic stuff to don´t make this to complex):

private void UpdateUser(User user)
{
    CrudService.UpdateUser(user, user.UserId);
}

And use that method with the delagete call (PassMethod), or you could use a anonymous method like this:

PassMethod(delegate { crudService.UpdateUser(user, user.UserId.ToString()); });

For some reason I prefer a nice Lambda syntax instead like this:

PassMethod(user, param => crudService.UpdateUser(user, user.UserId.ToString()));

Annonser