0

Why are delegates used here ?

I am reading this article. To learn about client-server communication https://www.codeproject.com/Articles/463947/Working-with-Sockets-in-Csharp I do understand what is happening till this line. AsyncCallback aCallback = new AsyncCallback(AcceptCallback); sListener.BeginAccept(aCallback, sListener); So "aCallback" is a delegate And there is a method AcceptCallback that is coupled that Delegate. (intermezzo question for native englisth people, what is beter english "a method that is" or "a method which is" ) Then this delegate is passed as parameter to a method. As far as I know delegate are a contract so all methods which want to communicate via this delegate follow the same definition structure. Why is this delegate created here ? Why not use the method itself ? like sListener.BeginAccept(AcceptCallback, sListener); Another question sListener.BeginAccept(aCallback, sListener); Why is the second parameter, the object itself ?

10/15/2018 9:21:14 PM

sneeze

1 Answer

New Answer

0

Hi sneeze, For the first question, I think that it's not possible to pass a method as parameter without create a delegate before (which type of object the function BeginAccept accept as first parameter?) For the second question, you must see the implementation of method BeginAccept of the socket class. Maybe It could reuse the sListener inside.