shashwat
shashwat

Reputation: 8024

Need explanation of using lambda as event handler

I found this piece of code in an answer on stackoverflow. htmlWeb.PreRequest does not seem to be an event handler because += tab tab is not generating required code and its symbol in IntelliSense tells me that it is a normal member variable.

Please explain this syntax. Is this an event handler or is it something else? I have seen += is usually used for adding event handlers. I searched on Google for terms like "ways to add event handler in c#" but couldn't find any such code.

Please help me understand this code. I understand the meaning of this code but not the syntax. Any other example would be appreciated.

HtmlWeb htmlWeb = new HtmlWeb();
htmlWeb.PreRequest += request =>
{
    request.CookieContainer = new System.Net.CookieContainer();
    return true;
};

Upvotes: 2

Views: 278

Answers (3)

Tieson T.
Tieson T.

Reputation: 21236

PreRequest is not a method, but rather a delegate:

http://htmlagilitypack.codeplex.com/SourceControl/latest#Trunk/HtmlAgilityPack/HtmlWeb.cs

So the lambda you've shown simply is assigning an anonymous method to said delegate that is used internally by HtmlAgilityPack to determine if some extra work is needed to correctly process the current request object, as shown here:

if (PreRequest != null)
{
    // allow our user to change the request at will
    if (!PreRequest(req))
    {
        return HttpStatusCode.ResetContent;
    }
}

When the condition if(!PreRequest(req))... is evaluated, it uses the anonymous method, which adds some extra "stuff" to the current request object, and then returns true (which ! then negates), so HAP knows to not return quite yet.

In case it's not all that clear, you don't supply the request object. HAP does that in the Get() method in which this delegate is being used:

req = WebRequest.Create(uri) as HttpWebRequest;

Your delegate is basically just supplying a method body to potentially do something to that request and then return either true or false.

Upvotes: 2

horgh
horgh

Reputation: 18553

Your code:

HtmlWeb htmlWeb = new HtmlWeb();
htmlWeb.PreRequest += request =>
{
    request.CookieContainer = new System.Net.CookieContainer();
    return true;
};

actually is quivalent to:

bool PreRequest_EventHandler(HttpWebRequest request)
{
    request.CookieContainer = new System.Net.CookieContainer();
    return true;
}
//...
HtmlWeb htmlWeb = new HtmlWeb();
htmlWeb.PreRequest += PreRequest_EventHandler;

The key difference is that it uses lambda expressions syntax instead of declaring a separate method. As it's said in the linked MSDN artcile lambda syntax has the following form:

(input parameters) => expression

So request => in your code is the input parameter. The brackets are omitted as there is only one parameter. If there were two or more, it would be (x,y)=>....

Upvotes: 5

Adriaan Stander
Adriaan Stander

Reputation: 166576

This is an example of an Anonymous Methods (C# Programming Guide)

In versions of C# previous to 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduces anonymous methods.

Creating anonymous methods is essentially a way to pass a code block as a delegate parameter.

By using anonymous methods, you reduce the coding overhead in instantiating delegates by eliminating the need to create a separate method.

So, basically, this is using an Anonymous method as the event handler.

Upvotes: 0

Related Questions