Was events keyword necessary?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I find that when i define a delegate, it gets derived from
MulticastDelegate, which provides all funtionality that events would provide
(like registering new handlers etc.). Then, apart from being a bit more
neater, could there have been any other reason that an 'event' keyword became
necessary in C#?

Thanks in advance,
 
I believe that the reason for the keyword is just semantics, although there are some benefits. Designers group events together to
allow for quick "registration" of handlers at design time. Also, events do not have to be constructed as delegates require use of
the "new" keyword.

Visit the C# programmers reference on MSDN for more info:

http://msdn.microsoft.com/library/d...-us/csref/html/vcoriCProgrammersReference.asp

and more specifically

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfeventpg.asp
 
hi,

Delegate is somewhat same and more powerful than our old C++ functional
pointer.

Suppose if you want to cal that function through any event then you have to
go for custom event. and the keywrod event is must.
 
Yes...I think initialization with the new operator is the only major benefit...

Dave said:
I believe that the reason for the keyword is just semantics, although there are some benefits. Designers group events together to
allow for quick "registration" of handlers at design time. Also, events do not have to be constructed as delegates require use of
the "new" keyword.

Visit the C# programmers reference on MSDN for more info:

http://msdn.microsoft.com/library/d...-us/csref/html/vcoriCProgrammersReference.asp

and more specifically

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfeventpg.asp

--
Dave Sexton
(e-mail address removed) (remove BYE-SPAM)
[Please do not reply by email unless asked to]
-----------------------------------------------------------------------
Rakesh Rajan said:
Hi,

I find that when i define a delegate, it gets derived from
MulticastDelegate, which provides all funtionality that events would provide
(like registering new handlers etc.). Then, apart from being a bit more
neater, could there have been any other reason that an 'event' keyword became
necessary in C#?

Thanks in advance,
 
Rakesh Rajan said:
Yes...I think initialization with the new operator is the only major
benefit...

Its not quite that. Events and delegates are semantically different beasts.
An event provides an abstraction of the concept of an subscribable event, it
strictly allows you to subscribe and unsubscribe(add and remove), it does
not allow invocation or other delegate operations.

When you use a delegate, anyone can raise that event and anyone can clear
the delegate list at its whim. An event, on the other hand, only
allows(generally) access to add and remove functionality, thus you cannot
remove random delegates or randomly invoke delegates as you would be able to
with a literal delegate field.

Dave said:
I believe that the reason for the keyword is just semantics, although
there are some benefits. Designers group events together to
allow for quick "registration" of handlers at design time. Also, events
do not have to be constructed as delegates require use of
the "new" keyword.

Visit the C# programmers reference on MSDN for more info:

http://msdn.microsoft.com/library/d...-us/csref/html/vcoriCProgrammersReference.asp

and more specifically

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfeventpg.asp

--
Dave Sexton
(e-mail address removed) (remove BYE-SPAM)
[Please do not reply by email unless asked to]
-----------------------------------------------------------------------
Rakesh Rajan said:
Hi,

I find that when i define a delegate, it gets derived from
MulticastDelegate, which provides all funtionality that events would
provide
(like registering new handlers etc.). Then, apart from being a bit more
neater, could there have been any other reason that an 'event' keyword
became
necessary in C#?

Thanks in advance,
 
I agree with Daniel's description of the semantical differences between the events keyword and delegates declared as fields.

He has explained why the events keword was "necessary".

--
Dave Sexton
(e-mail address removed) (remove BYE-SPAM)
[Please do not reply by email unless asked to]
-----------------------------------------------------------------------
Daniel O'Connell said:
Rakesh Rajan said:
Yes...I think initialization with the new operator is the only major
benefit...

Its not quite that. Events and delegates are semantically different beasts.
An event provides an abstraction of the concept of an subscribable event, it
strictly allows you to subscribe and unsubscribe(add and remove), it does
not allow invocation or other delegate operations.

When you use a delegate, anyone can raise that event and anyone can clear
the delegate list at its whim. An event, on the other hand, only
allows(generally) access to add and remove functionality, thus you cannot
remove random delegates or randomly invoke delegates as you would be able to
with a literal delegate field.

Dave said:
I believe that the reason for the keyword is just semantics, although
there are some benefits. Designers group events together to
allow for quick "registration" of handlers at design time. Also, events
do not have to be constructed as delegates require use of
the "new" keyword.

Visit the C# programmers reference on MSDN for more info:

http://msdn.microsoft.com/library/d...-us/csref/html/vcoriCProgrammersReference.asp

and more specifically

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfeventpg.asp

--
Dave Sexton
(e-mail address removed) (remove BYE-SPAM)
[Please do not reply by email unless asked to]
-----------------------------------------------------------------------
Hi,

I find that when i define a delegate, it gets derived from
MulticastDelegate, which provides all funtionality that events would
provide
(like registering new handlers etc.). Then, apart from being a bit more
neater, could there have been any other reason that an 'event' keyword
became
necessary in C#?

Thanks in advance,
 
Clear as crystal...thanks all :)

Dave said:
I agree with Daniel's description of the semantical differences between the events keyword and delegates declared as fields.

He has explained why the events keword was "necessary".

--
Dave Sexton
(e-mail address removed) (remove BYE-SPAM)
[Please do not reply by email unless asked to]
-----------------------------------------------------------------------
Daniel O'Connell said:
Rakesh Rajan said:
Yes...I think initialization with the new operator is the only major
benefit...

Its not quite that. Events and delegates are semantically different beasts.
An event provides an abstraction of the concept of an subscribable event, it
strictly allows you to subscribe and unsubscribe(add and remove), it does
not allow invocation or other delegate operations.

When you use a delegate, anyone can raise that event and anyone can clear
the delegate list at its whim. An event, on the other hand, only
allows(generally) access to add and remove functionality, thus you cannot
remove random delegates or randomly invoke delegates as you would be able to
with a literal delegate field.

:

I believe that the reason for the keyword is just semantics, although
there are some benefits. Designers group events together to
allow for quick "registration" of handlers at design time. Also, events
do not have to be constructed as delegates require use of
the "new" keyword.

Visit the C# programmers reference on MSDN for more info:

http://msdn.microsoft.com/library/d...-us/csref/html/vcoriCProgrammersReference.asp

and more specifically

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfeventpg.asp

--
Dave Sexton
(e-mail address removed) (remove BYE-SPAM)
[Please do not reply by email unless asked to]
-----------------------------------------------------------------------
Hi,

I find that when i define a delegate, it gets derived from
MulticastDelegate, which provides all funtionality that events would
provide
(like registering new handlers etc.). Then, apart from being a bit more
neater, could there have been any other reason that an 'event' keyword
became
necessary in C#?

Thanks in advance,
 
Back
Top