What is this delegate call in this line of code (C #)?

This is an example related to the assembly of agsXMPP.Net. I read about delegates, but I don’t know how it fits into this line of code (which waits for the login and then sends a message. I assume that I am looking for this understanding of why delegate(0) does this in the form of simple concepts that I can understand.

 xmpp.OnLogin += delegate(object o) { xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); }; 
+4
source share
9 answers

delegate(object o){..} tells the compiler to pack everything inside the brackets as an object that will be executed later, in this case, when OnLogin starts. Without the delegate() operator, the compiler will think that you are binding the execution of the action in the middle of the assignemnt statement and giving you errors.

+1
source

It is just like

 xmpp.OnLogin += EventHandler(MyMethod); 

Where is mymethod

 public void MyMethod(object o) { xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); } 
+4
source

As Abe noted, this code creates an anonymous function. It:

 xmpp.OnLogin += delegate(object o) { xmpp.Send( new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); }; 

in earlier versions of .Net (class declarations, etc., would be excluded, and essential elements would simply be retained):

 delegate void OnLoginEventHandler(object o); public void MyLoginEventHandler(object o) { xmpp.Send( new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); } [...] xmpp.OnLogin += new OnLoginEventHandler(MyLoginEventHandler); 

What you do anyway is to bind your method, which will be triggered when the XMPP OnLogin event fires.

+2
source

OnLogin on xmpp is probably an event declared as follows:

 public event LoginEventHandler OnLogin; 

where LoginEventHandler as the delegate type is probably declared as:

 public delegate void LoginEventHandler(Object o); 

This means that in order to subscribe to the event, you need to provide a method (or anonymous method / lambda expression ) that correspond to the subscription of the LoginEventHandler delegate.

In your example, you are passing an anonymous method using the delegate keyword:

 xmpp.OnLogin += delegate(object o) { xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); }; 

An anonymous method matches the delegate signature expected by the OnLogin event (return type is void + one object). You can also remove the object o parameter using contravariance , since it is not used inside the body of an anonymous method.

 xmpp.OnLogin += delegate { xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); }; 
+2
source

This creates an anonymous function. This feature was introduced in C # 2.0

0
source

It serves as an anonymous method, so you do not need to declare it anywhere else. It is very useful.

In this case, he should attach this method to the list of actions that are triggered due to the onLogin event.

0
source

According to Abe, this is an anonymous method. An anonymous method is an unnamed method that can be represented as an argument to a parameter.

Obviously, the OnLogin object is an event; using the + = operator ensures that the method specified by the anonymous delegate above is executed whenever an OnLogin event occurs.

0
source

Basically, the code inside {} will run when the "OnLogin" event of the xmpp event fires. Based on the name, I would suggest that the event fires at some point during the login process.

Syntax:

 delegate(object o) { statements; } 

is an anonymous method. The code in your question would be equivalent to this:

 public class MyClass { private XMPPObjectType xmpp; public void Main() { xmpp.OnLogin += MyMethod; } private void MyMethod(object o) { xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); } } 
0
source

You are subscribed to the OnLogin event in xmpp.

This means that when xmpp fires this event, the code inside the anonymous delegate lights up. Its an elegant way to have callbacks.

In Xmpp, the following happens:

  // Check to see if we should fire the login event // ALso check to see if anything is subscribed to OnLogin // (It will be null otherwise) if (loggedIn && OnLogin != null) { // Anyone subscribed will now receive the event. OnLogin(this); } 
0
source

All Articles