The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Delegates & Events - A short Q&A

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
Delegates & Events - A short Q&A Posted: Mar 27, 2004 7:28 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: Delegates & Events - A short Q&A
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement

The other day I gave a presentation about Delegates & events to folks who were new to C#. I dawned on me that in fact, this subject is kind of confusing, and several questions arose from this presentation.

Disclaimer: this is intended to be read by people already familiar with the Delegates and Events syntax, and is mostly related to C# syntax, since VB.Net syntax is a little different (simpler) in some cases.

 

Q:  What’s the difference between a Delegate and a MultiCastDelegate?

A: There is no real difference. Every delegate is a MultiCastDelegate, but in order to be logically called a MultiCastDelegate, it has to have more than one target in its Invocation list (multiple targets==multiple cast).

So, in essence, once you have created a delegate, you've created a MultiCastDelegate with an invocation list count of 1.

 

Q: What’s the difference between a delegate and an event?

A: I’ll divide the answer into 2 parts.

Syntax wise:

Here’s how one could define a delegate in a class:

 

public class Class1

{

public delegate void DomSomethingDelegate(int num1,int num2);

public DomSomethingDelegate MyDeleageteCallback;

}

 

And here’s how I would declare an event of this delegate:

public class Class2

{

public delegate void DomSomethingDelegate(int num1,int num2);

public event DomSomethingDelegate MyDeleageteCallback;

}

 

Notice that syntactically, the only difference is that I place an “event” keyword before the delegate variable declaration. That’s it.

 

So what’s the “event” keyword adding to this?

Well, to understand this, consider Class1 in the code above. If I was using this class, as a client I could set it’s delegate variable like this:

Class1 c = new Class1();

c.MyDeleageteCallback+=new

Class1.DomSomethingDelegate(this.Calculate);

 

This simple code adds a new target to the delegate’s invocation list, which is perfectly fine. In fact, this same code will word with Class2. no difference what so ever so far. But consider this code which, instead of adding a new target to the delegate’s Invocation list, simply sets the delegate to a new delegate:

Class1 c = new Class1();

c.MyDeleageteCallback=new

Class1.DomSomethingDelegate(this.Calculate);

 

This piece of code will work just fine with class1, but if we were to try to use it on class2, where there is the event keyword declared, we would get a compilation error.

In essence, declaring the event keyword prevents any of the delegate’s users from setting it to null. Why is this important? Image that as a client I would add to the delegates invocation list a callback to one of my class’s functions. So would other clients. All is well and good. Now imagine that someone, instead of using the “+=”, is simply setting the delegate to a new callback by using simply “=”. They basically just threw the old delegate and its invocation list down the drain and created a whole new delegate with a single item in its invocation list. All the other clients will not receive their callbacks when the time comes. It is this kind of situation that having the  “event” keyword is aiming to solve.

Read: Delegates & Events - A short Q&A

Topic: VS.NET Extensions Web Site Previous Topic   Next Topic Topic: VS.NET Extensions Web Site

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use