473,387 Members | 1,569 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,387 software developers and data experts.

Events and delegates questions

1. A delegate is a type-safe, object-oriented function pointer.

2. A delegate declartion is C-sharp, f.e., public delegate void
MyDelegate(int); defines a MulticastDelgate which contains a linked-list of
delegates that can be added using .Combine method.

3. An event object is an implementation of the Observer design pattern.

4. An event is declared thus:

public event MyDelegate MyEvent;

5. An event is simply a wrapper around a MulticastDelegate, the C-sharp
compiler will generate static += and -= operators to more easily add or
subtract MulticastDelegates to the underlying MulticastDelegate.
Nov 16 '05 #1
1 2321
Hi Manco,
1. A delegate is a type-safe, object-oriented function pointer.
Yes. Event more due to the multicast nature of the delegates.

2. A delegate declartion is C-sharp, f.e., public delegate void
MyDelegate(int); defines a MulticastDelgate which contains a linked-list
of delegates that can be added using .Combine method.
No. It declares a class that inherits from MulticastDelegate. This class
declares strongly typed Invoke and BeginInvoke methods

3. An event object is an implementation of the Observer design pattern.
Yes.

4. An event is declared thus:

public event MyDelegate MyEvent;
This is one of the possible declarations.
The other one is
public event MyDelegate MyEvent
{
add
{
....
}
remove
{
....
}
}

The former declares one private field of the type of the delagate +
declaration and implementation of the add and remove accessors. The
visiblity of the accessors is what is provided in the event declaration.
The latter doesn't declare the backup field. It gives you the oportunity to
provide your own implementation of the accessors.

5. An event is simply a wrapper around a MulticastDelegate, the C-sharp
compiler will generate static += and -= operators to more easily add or
subtract MulticastDelegates to the underlying MulticastDelegate.


No. Events are pair of methods *add* and *remove*. By the way docs specify
one more called invoke, but it is not supported by C#. Actually the names
could be any names since those names are provided in the assemply meta data,
but .NET specifications recomends tha the names of these accessors are named
after the name of the event, for example in the case of event MyEvent they
are named add_MyEent and remove_MyEvent.

+= and -= on the other hand are not overloded any where they are compiled by
the C# compiler in a special way. If on the right hand side stays delegate
or event name. The former is trianslated as Delegate.Combine and
Delegate.Remove respectively method calls; the latter is translated as
add_XXX and remove_XXX method calls.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
Nov 16 '05 #2

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: Erik Ekedahl | last post by:
I am a intermediate VB6 programmer moving to VB.NET using the book "Programming Microsfot Visual Basic.net" (Microsoft Press) I have understood so far what is going on but I am haveing a bit of...
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...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
3
by: Jose Fernandez | last post by:
Hello. I would like to know how could i get all the subscriptions that my form has with the events of their controls. For example. I have a form with a textbox, a button and a dropdown. I...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.