<p>This is from an example accompanying the agsXMPP .Net assembly.  I've read up on delegates, but am not sure how that fits in with this line of code (which waits for the logon to occur, and then sends a message.  I guess what I'm looking for is an understanding of why <code>delegate(0)</code> accomplishes this, in the kind of simple terms I can understand.</p>

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

[{'answer_id': 73040, 'author': 'Abe Heidebrecht', 'author_id': 9268, 'author_profile': 'https://Stackoverflow.com/users/9268', 'pm_score': 0, 'selected': False, 'text': '<p>That is creating an anonymous function.  This feature was introduced in C# 2.0</p>\n'}, {'answer_id': 73066, 'author': 'André Chalella', 'author_id': 4850, 'author_profile': 'https://Stackoverflow.com/users/4850', 'pm_score': 0, 'selected': False, 'text': "<p>It serves as an anonymous method, so you don't need to declare it somewhere else. It's very useful.</p>\n\n<p>What it does in that case is to attach that method to the list of actions that are triggered because of the <code>onLogin</code> event.</p>\n"}, {'answer_id': 73069, 'author': 'juan', 'author_id': 1782, 'author_profile': 'https://Stackoverflow.com/users/1782', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s exactly the same as</p>\n\n<pre><code>xmpp.OnLogin += EventHandler(MyMethod);\n</code></pre>\n\n<p>Where MyMethod is</p>\n\n<pre><code>public void MyMethod(object o) \n{ \n    xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); \n}\n</code></pre>\n'}, {'answer_id': 73083, 'author': 'Gilligan', 'author_id': 12356, 'author_profile': 'https://Stackoverflow.com/users/12356', 'pm_score': 2, 'selected': True, 'text': '<p>The <code>delegate(object o){..}</code> tells the compiler to package up whatever is inside the brackets as an object to be executed later, in this case when <code>OnLogin</code> is fired. Without the <code>delegate()</code> statement, the compiler would think you are tying to execute an action in the middle of an assignemnt statement and give you errors.</p>\n'}, {'answer_id': 73088, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': '<p>Agreed with Abe, this is an anonymous method. An anonymous method is just that -- a method without a name, which can be supplied as a parameter argument.</p>\n\n<p>Obviously the OnLogin object is an Event; using an += operator ensures that the method specified by the anonymous delegate above is executed whenever the OnLogin event is raised.</p>\n'}, {'answer_id': 73092, 'author': 'Jonathan Rupp', 'author_id': 12502, 'author_profile': 'https://Stackoverflow.com/users/12502', 'pm_score': 0, 'selected': False, 'text': '<p>Basically, the code inside the {} will run when the "OnLogin" event of the xmpp event is fired.  Based on the name, I\'d guess that event fires at some point during the login process.</p>\n\n<p>The syntax:</p>\n\n<pre><code>delegate(object o) { statements; }\n</code></pre>\n\n<p>is a called an anonymous method.  The code in your question would be equivilent to this:</p>\n\n<pre><code>public class MyClass\n{\n  private XMPPObjectType xmpp;\n  public void Main()\n  {\n    xmpp.OnLogin += MyMethod;\n  }\n  private void MyMethod(object o)\n  {\n    xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?"));\n  }\n}\n</code></pre>\n'}, {'answer_id': 73103, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': '<p>You are subscribing to the OnLogin event in xmpp.</p>\n\n<p>This means that when xmpp fires this event, the code inside the anonymous delegate will fire. Its an elegant way to have callbacks.</p>\n\n<p>In Xmpp, something like this is going on:</p>\n\n<pre><code>   // Check to see if we should fire the login event\n   // ALso check to see if anything is subscribed to OnLogin \n   // (It will be null otherwise)\n   if (loggedIn &amp;&amp; OnLogin != null)\n   {\n       // Anyone subscribed will now receive the event.\n       OnLogin(this);\n   }\n</code></pre>\n'}, {'answer_id': 73137, 'author': 'Remi Despres-Smyth', 'author_id': 8169, 'author_profile': 'https://Stackoverflow.com/users/8169', 'pm_score': 2, 'selected': False, 'text': '<p>As Abe noted, this code is creating an anonymous function.  This:</p>\n\n<pre><code>\nxmpp.OnLogin += delegate(object o) \n   { \n      xmpp.Send(\n         new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); \n   };\n</code></pre>\n\n<p>would have been accomplished as follows in older versions of .Net (I\'ve excluded class declarations and such, and just kept the essential elements):</p>\n\n<pre><code>\ndelegate void OnLoginEventHandler(object o);\n\npublic void MyLoginEventHandler(object o)\n{\n      xmpp.Send(\n         new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); \n}\n\n[...]\n\nxmpp.OnLogin += new OnLoginEventHandler(MyLoginEventHandler);\n</code></pre>\n\n<p>What you\'re doing in either case is associating a method of yours to run when the xmpp OnLogin event is fired.</p>\n'}, {'answer_id': 73254, 'author': 'Romain Verdier', 'author_id': 4687, 'author_profile': 'https://Stackoverflow.com/users/4687', 'pm_score': 2, 'selected': False, 'text': '<p><code>OnLogin</code> on xmpp is probably an event declared like this :</p>\n\n<pre><code>public event LoginEventHandler OnLogin;\n</code></pre>\n\n<p>where <code>LoginEventHandler</code> is as delegate type probably declared as :</p>\n\n<pre><code>public delegate void LoginEventHandler(Object o);\n</code></pre>\n\n<p>That means that in order to subscribe to the event, you need to provide a method (or an <a href="http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx" rel="nofollow noreferrer">anonymous method</a> / <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" rel="nofollow noreferrer">lambda expression</a>) which match the <code>LoginEventHandler</code> delegate signature.</p>\n\n<p>In your example, you pass an anonymous method using the <code>delegate</code> keyword:</p>\n\n<pre><code>xmpp.OnLogin += delegate(object o)\n                { \n                    xmpp.Send(new Message(new Jid(JID_RECEIVER), \n                              MessageType.chat,\n                              "Hello, how are you?")); \n                };\n</code></pre>\n\n<p>The anonymous method matches the delegate signature expected by the <code>OnLogin</code> event (void return type + one object argument). You could also remove the <code>object o</code> parameter leveraging the <a href="http://msdn.microsoft.com/en-us/library/ms173174(VS.80).aspx" rel="nofollow noreferrer">contravariance</a>, since it is not used inside the anonymous method body.</p>\n\n<pre><code>xmpp.OnLogin += delegate\n                { \n                    xmpp.Send(new Message(new Jid(JID_RECEIVER), \n                              MessageType.chat,\n                              "Hello, how are you?")); \n                };\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/73024', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7574/']