这个代理呼叫在这行代码(C#)中做了什么? [英] What is this delegate call doing in this line of code (C#)?

查看:159
本文介绍了这个代理呼叫在这行代码(C#)中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从agsXMPP .Net程序集附带的一个例子。我已经阅读了代表,但我不知道如何适应这行代码(等待登录发生,然后发送一个消息,我想我正在寻找的是理解为什么 delegate(0)以这种简单的术语我可以理解。

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 delegate(0) accomplishes this, in the kind of simple terms I can understand.

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


推荐答案

delegate(object o){..} 告诉编译器将括号内的任何内容打包成稍后要执行的对象,在这种情况下,当 OnLogin 被触发,没有 delegate()语句,编译器会认为你正在绑定在一个assignemnt语句的中间执行一个动作并给你错误。

The delegate(object o){..} tells the compiler to package up whatever is inside the brackets as an object to be executed later, in this case when OnLogin is fired. Without the delegate() statement, the compiler would think you are tying to execute an action in the middle of an assignemnt statement and give you errors.

这篇关于这个代理呼叫在这行代码(C#)中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆