473,473 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

delegate and "+=" operator

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);
}
}
Nov 15 '05 #1
13 2995
José Joye <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> wrote:
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?


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
Nov 15 '05 #2
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:
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?


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
Nov 15 '05 #3
100
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...
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);
}
}

Nov 15 '05 #4
100,

Actually, the IL reflects the BeginInvoke, EndInvoke and Invoke methods.
They are not inferred by intellisense, but rather, they are picked up as any
other method in an assembly.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"100" <10*@100.com> wrote in message
news:OD**************@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...
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);
}
}


Nov 15 '05 #5
José Joye <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> wrote:
The ECMA specs sound more detailled than MSDN...
MSDN has the C# specs too. It's in section 7.7.4 in the MSDN version.
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#?


Events allow other classes to subscribe and unsubscribe, but not
replace completely (either with null or a single delegate), or invoke
the delegates which have been subscribed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
I still not fully get the point so far. As I see it, whatever an event can
do can be done be a delegate.
Am I correct to say that?

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:
The ECMA specs sound more detailled than MSDN...
MSDN has the C# specs too. It's in section 7.7.4 in the MSDN version.
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#?


Events allow other classes to subscribe and unsubscribe, but not
replace completely (either with null or a single delegate), or invoke
the delegates which have been subscribed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
José Joye <jose.joye@__No_SPam__bluewin__maPS_oN__.ch> wrote:
I still not fully get the point so far. As I see it, whatever an event can
do can be done be a delegate.
Am I correct to say that?


Yes - but the reverse isn't true, which is the point. You often want to
make events public to other classes, but making the *delegate* public
would be potentially nasty, as other classes could trample on each
other's handlers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Got the point.

Thanks!
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:
I still not fully get the point so far. As I see it, whatever an event can
do can be done be a delegate.
Am I correct to say that?


Yes - but the reverse isn't true, which is the point. You often want to
make events public to other classes, but making the *delegate* public
would be potentially nasty, as other classes could trample on each
other's handlers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
100
Yes, they are reflected because they exist at runtime. How I said they are
synthesized by CLR during type loading.
If you open mscorlib.dll in ILDasm you'll see that thre are no such methods.
And because they are not defined in Delegate class source they are not it
MSDN.

B\rgds
100
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uq**************@TK2MSFTNGP12.phx.gbl...
100,

Actually, the IL reflects the BeginInvoke, EndInvoke and Invoke methods. They are not inferred by intellisense, but rather, they are picked up as any other method in an assembly.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"100" <10*@100.com> wrote in message
news:OD**************@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...
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);
}
}



Nov 15 '05 #10
100 <10*@100.com> wrote:
Yes, they are reflected because they exist at runtime. How I said they are
synthesized by CLR during type loading.
No they're not.
If you open mscorlib.dll in ILDasm you'll see that thre are no such methods.
And because they are not defined in Delegate class source they are not it
MSDN.


Nope - they're constructed by the C# compiler. Here's an example:

using System;

public class Test
{
public delegate void Foo();

// Just for ease of compilation
static void Main()
{
}
}

Compile that, and look at test.exe with ildasm - you'll see that Foo
has BeginInvoke and EndInvoke defined. Unfortunately this, like the
hidden event naming scheme, isn't specified by the standard :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
Since delegates are basically a collection of methods to invoke, why dont we
have the usual colletion operations? .Add and .Remove , we have a
..Combine.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
100 <10*@100.com> wrote:
Yes, they are reflected because they exist at runtime. How I said they are synthesized by CLR during type loading.


No they're not.
If you open mscorlib.dll in ILDasm you'll see that thre are no such methods. And because they are not defined in Delegate class source they are not it MSDN.


Nope - they're constructed by the C# compiler. Here's an example:

using System;

public class Test
{
public delegate void Foo();

// Just for ease of compilation
static void Main()
{
}
}

Compile that, and look at test.exe with ildasm - you'll see that Foo
has BeginInvoke and EndInvoke defined. Unfortunately this, like the
hidden event naming scheme, isn't specified by the standard :(

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

Nov 15 '05 #12
100
Hi Jon,

Did you try this? Did see that those methods doesn't have body?
They are marked as *runtime* in the method implmentation flags and RVA of
the IL code is 0
Which means that those methods doesn't have implemenation and the
implementaion is provided by CLR (generated by CLR to fit the method
prototype).

May be I was unclear. C# generates only the prototype of those methods.
Their code, though, is not generated by the compiler.
As far as I understand all .NET compilers has to provide those methods, so
they could be mentioned along with the other methods of Delegate class not
only on *about Delegate class* section of MSDN.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
100 <10*@100.com> wrote:
Yes, they are reflected because they exist at runtime. How I said they are synthesized by CLR during type loading.


No they're not.
If you open mscorlib.dll in ILDasm you'll see that thre are no such methods. And because they are not defined in Delegate class source they are not it MSDN.


Nope - they're constructed by the C# compiler. Here's an example:

using System;

public class Test
{
public delegate void Foo();

// Just for ease of compilation
static void Main()
{
}
}

Compile that, and look at test.exe with ildasm - you'll see that Foo
has BeginInvoke and EndInvoke defined. Unfortunately this, like the
hidden event naming scheme, isn't specified by the standard :(

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

Nov 15 '05 #13
100 <10*@100.com> wrote:
Did you try this?
Yes.
Did see that those methods doesn't have body?
I hadn't seen that, no :(
They are marked as *runtime* in the method implmentation flags and RVA of
the IL code is 0
Which means that those methods doesn't have implemenation and the
implementaion is provided by CLR (generated by CLR to fit the method
prototype).
Yup.
May be I was unclear. C# generates only the prototype of those methods.
Their code, though, is not generated by the compiler.
Right. Sorted :)
As far as I understand all .NET compilers has to provide those methods, so
they could be mentioned along with the other methods of Delegate class not
only on *about Delegate class* section of MSDN.


Right. I still think it should be in the C# ECMA spec...

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stephen Johns | last post by:
I want to have a Hashtable whose keys are strings and whose values are delegates. Well, ok, I have that: dels = new Hastable(); dels.Add( "key", new Foo1Delegate(MyFoo1) ); dels.Add( "key",...
5
by: han zhiyang | last post by:
Hi. I tried to design a custom web control which can flexibly and dynamicly let the control user ,for example the web page developer, customize its layout codes.This control derives from...
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.