Alexander Pope
Alexander Pope

Reputation: 1224

Should I specify the delegate when passing a callback?

I was looking through some tutorials on async web requests using HttpWebRequest and came upon this example: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream%28v=vs.100%29.aspx.

At request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request); the delegate is explicitly stated, wrapping the callback. Is there any advantage in doing this, compared to: request.BeginGetRequestStream(GetRequestStreamCallback, request); ?

Upvotes: 1

Views: 46

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039000

Both codes are absolutely equivalent and it will boil down to the same IL. The C# compiler is sufficiently advanced enough to infer the proper type in the second case. So basically it's a matter of personal preference whether you prefer to write more characters than necessary and be more explicit.

Upvotes: 2

Related Questions