473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Events versus Delegates ??

Hi,

what is the added value of using the 'event' keyword ?

have a look at following (working) code please
(you can just copy/paste and build it) :

using System;
public delegate void WakeUpDelegate();
class AClass
{
public static void WakeUpA()
{
Console.WriteLine("WakeUpA()");
}
}
class Timer
{
public WakeUpDelegate deleg;
public void Alarm()
{
if ( null != deleg )
deleg();
}
}

class Program
{
public static void Main()
{
Timer objTimer = new Timer();
objTimer.deleg += new WakeUpDelegate(AClass.WakeUpA);

objTimer.Alarm();
}
}

My question is :
in class Timer , whether I declare
public WakeUpDelegate deleg;
or
public event WakeUpDelegate deleg;

there is no difference when I run the program, so ...
... what is the added value of using the 'event' keyword ?

When to use events ? When to use just delegates ?

Thnx

Chris
Nov 15 '05 #1
8 1544
Sam
The main difference is that in your code the following is
completely valid...

objTimer.deleg();

i.e. anyone can trigger the event, including other
classes or assemblies. (Imagine that anyone could trigger
a disconnected event from say a web browser to tell
everyone listened that they'd been disconnected when in
fact that hadn't happened!)

With the event keyword the above wont compile and the
event can only be triggered from inside class in
question - i.e. the only class that should know if the
event has actually happened.

Sam
-----Original Message-----
Hi,

what is the added value of using the 'event' keyword ?

have a look at following (working) code please
(you can just copy/paste and build it) :

using System;
public delegate void WakeUpDelegate();
class AClass
{
public static void WakeUpA()
{
Console.WriteLine("WakeUpA()");
}
}
class Timer
{
public WakeUpDelegate deleg;
public void Alarm()
{
if ( null != deleg )
deleg();
}
}

class Program
{
public static void Main()
{
Timer objTimer = new Timer();
objTimer.deleg += new WakeUpDelegate (AClass.WakeUpA);
objTimer.Alarm();
}
}

My question is :
in class Timer , whether I declare
public WakeUpDelegate deleg;
or
public event WakeUpDelegate deleg;

there is no difference when I run the program, so ...
... what is the added value of using the 'event' keyword ?
When to use events ? When to use just delegates ?

Thnx

Chris
.

Nov 15 '05 #2
A couple of things. First, as a language feature, you can publically expose
an event field, and clients of your class/struct will only be able to use
the += and -= operators against the event.

Second, as an idiom, an event is more contrained than the delegate that
underlies it. Although a delegate can have any number of parameters and can
return a value, an event traditionally follows this pattern:
- always returns void
- always passes two parameters
- the first parameter is a reference to the logical sender
- the second parameter is EventArgs (or a subclass of EventArgs)

These naming constraints are not enforced by the runtime or C# - it's just
The Way We Do Things. If you misbehave, we'll mock you. But seriously, when
you mark a delegate as an event, you're implying that methods attached to
that delegate are invoked one-way, using a predictable notification pattern.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Chris" <ch********@pandora.be> wrote in message
news:AP*********************@phobos.telenet-ops.be...
Hi,

what is the added value of using the 'event' keyword ?

have a look at following (working) code please
(you can just copy/paste and build it) :

using System;
public delegate void WakeUpDelegate();
class AClass
{
public static void WakeUpA()
{
Console.WriteLine("WakeUpA()");
}
}
class Timer
{
public WakeUpDelegate deleg;
public void Alarm()
{
if ( null != deleg )
deleg();
}
}

class Program
{
public static void Main()
{
Timer objTimer = new Timer();
objTimer.deleg += new WakeUpDelegate(AClass.WakeUpA);

objTimer.Alarm();
}
}

My question is :
in class Timer , whether I declare
public WakeUpDelegate deleg;
or
public event WakeUpDelegate deleg;

there is no difference when I run the program, so ...
... what is the added value of using the 'event' keyword ?

When to use events ? When to use just delegates ?

Thnx

Chris

Nov 15 '05 #3
Why are they synchronous and not asynchronous?

Fire and forget.

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:#$**************@TK2MSFTNGP11.phx.gbl...
A couple of things. First, as a language feature, you can publically expose an event field, and clients of your class/struct will only be able to use
the += and -= operators against the event.

Second, as an idiom, an event is more contrained than the delegate that
underlies it. Although a delegate can have any number of parameters and can return a value, an event traditionally follows this pattern:
- always returns void
- always passes two parameters
- the first parameter is a reference to the logical sender
- the second parameter is EventArgs (or a subclass of EventArgs)

These naming constraints are not enforced by the runtime or C# - it's just
The Way We Do Things. If you misbehave, we'll mock you. But seriously, when you mark a delegate as an event, you're implying that methods attached to
that delegate are invoked one-way, using a predictable notification pattern.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Chris" <ch********@pandora.be> wrote in message
news:AP*********************@phobos.telenet-ops.be...
Hi,

what is the added value of using the 'event' keyword ?

have a look at following (working) code please
(you can just copy/paste and build it) :

using System;
public delegate void WakeUpDelegate();
class AClass
{
public static void WakeUpA()
{
Console.WriteLine("WakeUpA()");
}
}
class Timer
{
public WakeUpDelegate deleg;
public void Alarm()
{
if ( null != deleg )
deleg();
}
}

class Program
{
public static void Main()
{
Timer objTimer = new Timer();
objTimer.deleg += new WakeUpDelegate(AClass.WakeUpA);

objTimer.Alarm();
}
}

My question is :
in class Timer , whether I declare
public WakeUpDelegate deleg;
or
public event WakeUpDelegate deleg;

there is no difference when I run the program, so ...
... what is the added value of using the 'event' keyword ?

When to use events ? When to use just delegates ?

Thnx

Chris


Nov 15 '05 #4
Alvin,

Every delegate exposes a BeginInvoke method. You can call this to
fire-and-forget. Just don't set a callback for the call when it is made.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alvin Bruney" <alvin.bruney@.telia..com.> wrote in message
news:uD**************@TK2MSFTNGP11.phx.gbl...
Why are they synchronous and not asynchronous?

Fire and forget.

"Mickey Williams" <my first name at servergeek.com> wrote in message
news:#$**************@TK2MSFTNGP11.phx.gbl...
A couple of things. First, as a language feature, you can publically

expose
an event field, and clients of your class/struct will only be able to use the += and -= operators against the event.

Second, as an idiom, an event is more contrained than the delegate that
underlies it. Although a delegate can have any number of parameters and

can
return a value, an event traditionally follows this pattern:
- always returns void
- always passes two parameters
- the first parameter is a reference to the logical sender
- the second parameter is EventArgs (or a subclass of EventArgs)

These naming constraints are not enforced by the runtime or C# - it's just The Way We Do Things. If you misbehave, we'll mock you. But seriously,

when
you mark a delegate as an event, you're implying that methods attached to that delegate are invoked one-way, using a predictable notification

pattern.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Chris" <ch********@pandora.be> wrote in message
news:AP*********************@phobos.telenet-ops.be...
Hi,

what is the added value of using the 'event' keyword ?

have a look at following (working) code please
(you can just copy/paste and build it) :

using System;
public delegate void WakeUpDelegate();
class AClass
{
public static void WakeUpA()
{
Console.WriteLine("WakeUpA()");
}
}
class Timer
{
public WakeUpDelegate deleg;
public void Alarm()
{
if ( null != deleg )
deleg();
}
}

class Program
{
public static void Main()
{
Timer objTimer = new Timer();
objTimer.deleg += new WakeUpDelegate(AClass.WakeUpA);

objTimer.Alarm();
}
}

My question is :
in class Timer , whether I declare
public WakeUpDelegate deleg;
or
public event WakeUpDelegate deleg;

there is no difference when I run the program, so ...
... what is the added value of using the 'event' keyword ?

When to use events ? When to use just delegates ?

Thnx

Chris



Nov 15 '05 #5
"Alvin Bruney" <alvin.bruney@.telia..com.> wrote in message
news:uD**************@TK2MSFTNGP11.phx.gbl...
Why are they synchronous and not asynchronous?


Because in the general case the added complexity (due to concurrency safety
issues) and cost of asynchronous invocation overrides the scalability win.
As Nicholas points out, you can always use the asynchronous invocation
pattern against a delegate if you want true FNF semantics. If you take the
explicit step of requesting async invocation, the expectation is that you're
aware of issues like concurrency.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
Nov 15 '05 #6
The best coverage of this topic has been from Jesse Liberty. Here's a sample
chapter about events and delegates from the 3rd edition of his Programming
C# book: http://www.oreilly.com/catalog/progc...apter/ch12.pdf.

Scott
what is the added value of using the 'event' keyword ?

Nov 15 '05 #7
I agree completely. But I'd like to note that the .NET framework doesn't
always follow these rules. For example, take a look at the
AppDomain.AssemblyResolve event. The ResolveEventHandler returns an
Assembly. Sort of surprised me when I saw it. I would have expected the
Framework designers to put the return value into the event arguments. Oh
well, I guess even Microsoft can't police everything their developers do.

Tom Clement
Apptero, Inc.
"Mickey Williams" <my first name at servergeek.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
A couple of things. First, as a language feature, you can publically expose an event field, and clients of your class/struct will only be able to use
the += and -= operators against the event.

Second, as an idiom, an event is more contrained than the delegate that
underlies it. Although a delegate can have any number of parameters and can return a value, an event traditionally follows this pattern:
- always returns void
- always passes two parameters
- the first parameter is a reference to the logical sender
- the second parameter is EventArgs (or a subclass of EventArgs)

These naming constraints are not enforced by the runtime or C# - it's just
The Way We Do Things. If you misbehave, we'll mock you. But seriously, when you mark a delegate as an event, you're implying that methods attached to
that delegate are invoked one-way, using a predictable notification pattern.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Chris" <ch********@pandora.be> wrote in message
news:AP*********************@phobos.telenet-ops.be...
Hi,

what is the added value of using the 'event' keyword ?

have a look at following (working) code please
(you can just copy/paste and build it) :

using System;
public delegate void WakeUpDelegate();
class AClass
{
public static void WakeUpA()
{
Console.WriteLine("WakeUpA()");
}
}
class Timer
{
public WakeUpDelegate deleg;
public void Alarm()
{
if ( null != deleg )
deleg();
}
}

class Program
{
public static void Main()
{
Timer objTimer = new Timer();
objTimer.deleg += new WakeUpDelegate(AClass.WakeUpA);

objTimer.Alarm();
}
}

My question is :
in class Timer , whether I declare
public WakeUpDelegate deleg;
or
public event WakeUpDelegate deleg;

there is no difference when I run the program, so ...
... what is the added value of using the 'event' keyword ?

When to use events ? When to use just delegates ?

Thnx

Chris


Nov 15 '05 #8

"Chris" wrote:

When to use events ? When to use just delegates ?


Using delegates is useful when you just need a callback. Take a look
at the System.Threading.Timer class. An instance of this class
doesn't use event notification when a timer event occurs. Instead,
it calls a user defined callback (delegate) that was passed to it at
its creation. Note that this delegate is not made public and cannot
be changed for the lifetime of the object.

I assume this was done because using a delegate as a callback
instead of an event is better for performance reasons? If so, it
probably makes more sense to use a delegate instead of an event when
you want to avoid the overhead of the event mechanism but need a
callback for some kind of notification. In those cases, though, for
the reasons mentioned in this thread, it's not a good idea to make
the delegate public, but better to have it passed to the object when
it is created.

<Disclaimer>
Actually, I'm making some asumptions with what I said above, so I'd
appreciate any comments from those who have more knowledge about
this as it's something I've wondered about myself.
</Disclaimer>

Nov 15 '05 #9

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

Similar topics

4
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
6
by: kalishandy | last post by:
I have the following code to tie up a custom delegate (ConsoleDelegate) with two methods from a separate class called CallMyEvents: static void Main() { CallMyEvents call = new CallMyEvents();...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
What are the different features (in VB.NET) between a) Custom Events and b) Multicast Delegates to raise events? On the bottom line my class ad a) manages a list of handlers or ad b) obtains a...
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...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.