0

I have used lambda syntax before but I keep seeing the following kind of syntax and I am not sure how to interpret this. Are there are more conventional ways of writing these so I can compare the two and understand better?

This is one of the examples I have seen:

client.ExecuteAsync(request, (response, asyncHandle) =>
{
    Assert.NotNull(response.Content);
    Assert.Equal(val, response.Content);
    resetEvent.Set();
});

This is another example:

client.SearchAsync("Getting", s => {
    Assert.IsNotNull(s);
    Assert.AreEqual(1, s.Count);
  },
  Assert.IsNull);

Is there a way of writing these without lambda so I can understand them?

10
  • It's called statement lambda. See msdn.microsoft.com/en-us/library/bb397687.aspx Commented Nov 25, 2014 at 20:10
  • 2
    I don't understand what you are asking. Those lambdas are nearly identical, one just skipped the () around the parameters. Commented Nov 25, 2014 at 20:10
  • I have updated the question, the 2 statements are the ones I am trying to understand, they are from different code base. Commented Nov 25, 2014 at 20:13
  • That is the conventional way of writing that. There are more verbose methods of writing that, which may be more familiar to you, but it depends on what you're used to. Commented Nov 25, 2014 at 20:13
  • When you say writing these conventionally, do you mean without using a lamba? Commented Nov 25, 2014 at 20:14

1 Answer 1

3

In this example, Lambdas are like methods. This is a roughly equivalent code:

private SomeMethod(List<string> s)
{
    Assert.IsNotNull(s);
    Assert.AreEqual(1, s.Count);
}

clientSearchAsync("Getting", SomeMethod, Assert.IsNull);

In a nutshell, you're passing SomeMethod to SearchAsync method as a parameter, and SearhAsync invokes it in its body.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.