Delegates with out parameter

  • Thread starter Thread starter Amit Bansal \(MCT, MCSD.NET\)
  • Start date Start date
A

Amit Bansal \(MCT, MCSD.NET\)

What is meant by this statement?

"Although the delegate can use an out parameter, it is not recommended to
use it with multicast event delegates because there is no way to know which
delegate will be called."

regards
AB
 
Amit,

Basically, the idea is that if you have an out parameter on a delegate,
you can't be sure of what that out parameter is (or ref parameters, for that
matter).

Because a delegate can have multiple functions that are called when it
is invoked, if every routine set the out parameter to something different,
then you would lose the value set by all of the other routines.

Hope this helps.
 
Of course, you can always get the invocation list and call them individually
(and thus having each returned value in sequence), but I'm still not sure
that I would recommend it as an approach ;-p

Marc
 
Basically, when you write:

this.MyEvent += new MyEventDelegate(Function1);
this.MyEvent += new MyEventDelegate(Function2);

int a;
this.MyEvent(out a);

It's the equivalent of writing:

int a;
this.Function1(out a);
this.Function2(out a);

So, that the value written into a in Function1 is overwritten by the
value written by Function2. To make matters worse, you have no way of
knowing which function will be called first.
 
HI Nicholas,

Thanks for your mail. It has to do more with multicast delegates only. I
just spent some time on it:-

---------------
public delegate void Del(out string message);

public partial class OutDelegate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Del handler = DelegateMethod;
string name;
handler(out name);
Response.Write(name);

}

private void DelegateMethod(out string message)
{
message = "Amit";
}

}

-----------

Now in the above example, if 'handler' is calling a number methods of the
same signature, the parameter 'name' will have different value everytime. So
for every method it calls, there has to be a sepearte parameter to store the
value returned by the method call. So, the documentation says to avoid using
out parameter in case of multicast delegate. I hope I have understood it
right :)

Thanks
AB


Nicholas Paldino said:
Amit,

Basically, the idea is that if you have an out parameter on a delegate,
you can't be sure of what that out parameter is (or ref parameters, for that
matter).

Because a delegate can have multiple functions that are called when it
is invoked, if every routine set the out parameter to something different,
then you would lose the value set by all of the other routines.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Amit Bansal (MCT said:
What is meant by this statement?

"Although the delegate can use an out parameter, it is not recommended to
use it with multicast event delegates because there is no way to know
which
delegate will be called."

regards
AB
 
int a;
this.Function1(out a);
this.Function2(out a);

So, that the value written into a in Function1 is overwritten by the
value written by Function2. To make matters worse, you have no way of
knowing which function will be called first.

It seems much the same case as with a delegate that returns a value
(normal return, not an 'out'). But I thought that it was specified
that the last function named would determine the return value. Would
this not also be the case with 'out' params?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top