委托和“+ =”操作者 [英] delegate and "+=" operator

查看:65
本文介绍了委托和“+ =”操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后的例子让我感到困惑......

查看msdn文档,与

" delegate"相关的继承树如下:


对象< - 委托< - MulticastDelegate。

除非我是盲人,否则没有+运营商以及这个班级。


为了能够组合代表,我认为我们应该使用

MulticastDelegate或事件。


有人能指出我错过了哪些地方吗?

$ b $bJosé


========= =========================

class Class1

{

private delegate void myDelegate(string str);


[STAThread]

static void Main(string [] args)

{

myDelegate aDeleg = new myDelegate(m1);

myDelegate aDeleg1 = new myDelegate(m2);

aDeleg + = aDeleg1;

aDeleg(" Hello");

Console.ReadLine();

}


private static void m1(string str)

{

Console.WriteLine(" m1中收到{0},str);

}


private static void m2(string str)

{

Console.WriteLine("收到{0} in m2" ;,str);

}

}

The example at the end make me perplex...

Looking at the msdn documentation, the inheritance tree that relates to
"delegate" is as follow:

Object<--Delegate<--MulticastDelegate.
Unless I''m blind, there is no "+" operator as well for this class.

To be able to combine delegates, I thought we should use either
MulticastDelegate or event.

Could someone point me where I missed the point?

José

==================================
class Class1
{
private delegate void myDelegate (string str);

[STAThread]
static void Main(string[] args)
{
myDelegate aDeleg = new myDelegate(m1);
myDelegate aDeleg1 = new myDelegate(m2);
aDeleg += aDeleg1;
aDeleg("Hello");
Console.ReadLine();
}

private static void m1(string str)
{
Console.WriteLine ("Received {0} in m1", str);
}

private static void m2(string str)
{
Console.WriteLine ("Received {0} in m2", str);
}
}

推荐答案

JoséJoye< jose.joye @__ No_SPam__bluewin__maPS_oN __。ch>写道:
José Joye <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> wrote:
最后的例子让我感到困惑......

查看msdn文档,与
委托相关的继承树如下:

对象< - Delegate< - MulticastDelegate。
除非我失明,否则没有+操作员以及此课程。

为了能够组合代表,我认为我们应该使用
MulticastDelegate或事件。

有人能指出我在哪里错过了这一点?
The example at the end make me perplex...

Looking at the msdn documentation, the inheritance tree that relates to
"delegate" is as follow:

Object<--Delegate<--MulticastDelegate.
Unless I''m blind, there is no "+" operator as well for this class.

To be able to combine delegates, I thought we should use either
MulticastDelegate or event.

Could someone point me where I missed the point?




来自ECMA C#规范的第14.7.4节:


< quote>

每个委托类型隐式提供以下预定义的

运算符,其中D是委托类型:


D运算符+(D x,D Y);

< / quote>


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



From section 14.7.4 of the ECMA C# spec:

<quote>
Every delegate type implicitly provides the following predefined
operator, where D is the delegate type:

D operator +(D x, D y);
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


谢谢!


ECMA规格声音比MSDN更细致...


您的回答给我带来了另一个问题。在这一点上,我觉得

委托和事件的功能没有那么不同(+ =和

- =是支持,...)。

那么,在C#中使用两个关键字有什么意义?

$ b $bJosé


Jon Skeet [C#MVP]" < SK *** @ pobox.com>一个écritdansle message de

新闻:MP ************************ @ msnews.microsoft.c om .. 。
$ b $bJoséJoye< jose.joye @__ No_SPam__bluewin__maPS_oN __。ch>写道:
Thanks!

The ECMA specs sound more detailled than MSDN...

Your answer brought me another question. At this point, it sounds to me that
the functionallities of delegate and event are not that different ("+=" and
"-=" are supported, ...).
So, what is the point of having both keywords in C#?

José

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de
news:MP************************@msnews.microsoft.c om...
José Joye <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> wrote:
最后的例子让我感到困惑......

查看msdn文档,与
委托相关的继承树如下:

对象< - Delegate< - MulticastDelegate。
除非我失明,否则没有+操作员以及此课程。

为了能够组合代表,我认为我们应该使用
MulticastDelegate或事件。

有人能指出我在哪里错过了这一点?
The example at the end make me perplex...

Looking at the msdn documentation, the inheritance tree that relates to
"delegate" is as follow:

Object<--Delegate<--MulticastDelegate.
Unless I''m blind, there is no "+" operator as well for this class.

To be able to combine delegates, I thought we should use either
MulticastDelegate or event.

Could someone point me where I missed the point?




来自ECMA C#规范的第14.7.4节:


< quote>

每个委托类型隐式提供以下预定义的

运算符,其中D是委托类型:


D运算符+(D x,D y);

< / quote>


-

Jon Skeet - < sk *** @ pobox。 com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



From section 14.7.4 of the ECMA C# spec:

<quote>
Every delegate type implicitly provides the following predefined
operator, where D is the delegate type:

D operator +(D x, D y);
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


嗨Jose,

是的,那里是没有为委托声明的* + *或* - *操作。

* + = *和* - = *与delagates一起使用的操作由c#编译器处理。

它们可以被视为c#关键词S"添加amd将一个

委托移除给一个代理链。

编译器发出对Deleagte.Combine和Delegate.Remove的调用,或者万一
$ b $事件b调用* add *和* remove * accessors。


BTW,有一堆方法,由CLR运行时和

你在MSDN中的类方法中找不到它们。

这种方法的例子是Delegate的BeginInvoke和EndInvoke。如果你在
中查找他们在MSDN的Delagate类''成员中他们不在那里。

但是,它们存在并且IntelliSense显示它们。

HTH

B \ rgds




"JoséJoye" < jose.joye @__ No_SPam__bluewin__maPS_oN __ CH>。在消息中写道

news:uU ************** @ TK2MSFTNGP09.phx.gbl ...
Hi Jose,
Yes, there is no *+* or *-* operations declared for Delegates.
*+=* and *-=* operations used with delagates is handled by the c# compiler.
They can be considered as c# "key words" for adding amd removeing an
delegate to a chain of delegates.
The compiler emits calls to Deleagte.Combine and Delegate.Remove or in case
of events calls to *add* and *remove* accessors.

BTW, there is bunch of methods, which are run-time synthesized by CLR and
you won''t find them among class methods in MSDN.
Example for such methods are Delegate''s BeginInvoke and EndInvoke. If you
look for them in Delagate class'' members in MSDN they are not there.
However, they exist and IntelliSense shows them.

HTH
B\rgds
100

"José Joye" <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
最后的例子让我感到困惑......

查看msdn文档,与
委托相关的继承树。如下:

对象< - Delegate< - MulticastDelegate。
除非我失明,否则没有+操作员以及此课程。

为了能够组合代表,我认为我们应该使用
MulticastDelegate或事件。

有人能指出我在哪里错过了这一点?

José

============================= =====
class Class1
{private delegate void myDelegate(string str);

[STAThread]
static void Main(string [ ] args)
{
myDelegate aDeleg = new myDelegate(m1);
myDelegate aDeleg1 = new myDelegate(m2);
aDeleg + = aDeleg1;
aDeleg(" ; Hello");
Console.ReadLine();
}
私有静态void m1(string str)
{
Console.WriteLine(" ;在m1",str)中收到{0};
}

private static void m2(string str)
{
Console.WriteLine(" Received { 0} in m2",str);
}
}
The example at the end make me perplex...

Looking at the msdn documentation, the inheritance tree that relates to
"delegate" is as follow:

Object<--Delegate<--MulticastDelegate.
Unless I''m blind, there is no "+" operator as well for this class.

To be able to combine delegates, I thought we should use either
MulticastDelegate or event.

Could someone point me where I missed the point?

José

==================================
class Class1
{
private delegate void myDelegate (string str);

[STAThread]
static void Main(string[] args)
{
myDelegate aDeleg = new myDelegate(m1);
myDelegate aDeleg1 = new myDelegate(m2);
aDeleg += aDeleg1;
aDeleg("Hello");
Console.ReadLine();
}

private static void m1(string str)
{
Console.WriteLine ("Received {0} in m1", str);
}

private static void m2(string str)
{
Console.WriteLine ("Received {0} in m2", str);
}
}



这篇关于委托和“+ =”操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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