简体   繁体   English

C#Lambda语法

[英]C# lambda syntax

I have used lambda syntax before but I keep seeing the following kind of syntax, not sure how to interpret this, is there are more conventional way of writing these so I can compare the two and understand better. 我以前使用过lambda语法,但是我一直看到以下类型的语法,不确定如何解释,是否有更常规的编写方式,因此我可以比较两者并更好地理解。

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? 有没有lambda编写这些的方法,以便我可以理解它们?

Thanks 谢谢

In this example, Lambdas are like methods. 在此示例中,Lambda与方法类似。 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. 简而言之,您将SomeMethod作为参数传递给SearchAsync方法,SearhAsync在其主体中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM