Why are delegates used here ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 ?

15th Oct 2018, 9:21 PM
sneeze
sneeze - avatar
1 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.
28th Sep 2019, 9:19 PM
Gabriele Marra
Gabriele Marra - avatar