delegate problem

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hey

..NET 2.0

I'm trying to understand delegates. Below is an example I've created. I
don't understand how the delegate below becomes a reference pointer (AFIAK
delegates are reference pointers) to the method below it....

delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

any suggestions?
 
Jeff said:
I'm trying to understand delegates. Below is an example I've created. I
don't understand how the delegate below becomes a reference pointer (AFIAK
delegates are reference pointers) to the method below it....

delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

any suggestions?

The first line defines a delegate *type*. You need a new *instance* of
the delegate, e.g.

HelloWorldHandler foo = HelloWorld;
 
thanks, I understand that now

However there are something here I don't understand. I'm trying to
understand some .NET code using delegates. Below is another example which I
have trouble understanding. The example is taken from an app I'm programming
on (names on methods are changed):

delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

public int World(string name)
{
IAsyncResult aResult;
HelloWorldHandler handler = delegate(string name) {return decimal;};
aResult = handler.BeginInvoke(name, null, null, null);

if (!aResult.IsComplete)
aResult.AsyncWaitHandle.WaitOne();

decimal var1 =
((HelloWorldHandler)((AsyncResult)aResult).AsyncDelegate).EndInvoke(aResult);
}

There are lot of things I don't know here. Here no HelloWorldHandler foo =
HelloWorld; are used.. how can it still know what method to execute??

AFIAK the example above creates a delegate which execute in it's own thread.
started by BeginInvoke. and aResult.IsComplete check if the method reference
by the delegate is finished. And EndInvoke is called to retrieve the result
from the method.

Any suggestions?
 
The first line defines a delegate *type*. You need a new *instance* of
the delegate, e.g.

HelloWorldHandler foo = HelloWorld;

Incidently, Jon used Delegate Inference in the above.

HelloWorldHandler foo = HelloWorld;
would be:
HelloWorldHandler foo = new HelloWorldHandler(HelloWorld);
under 1.1, but it works in 2+ as well and you may well see both.
 
There are lot of things I don't know here. Here no HelloWorldHandler foo =
HelloWorld; are used.. how can it still know what method to execute??

It's using an anonymous method, here:

HelloWorldHandler handler = delegate(string name) {return decimal;};

Anonymous methods are a C# 2 features. See
http://pobox.com/~skeet/csharp/csharp2/delegates.html
AFIAK the example above creates a delegate which execute in it's own thread.

Well, it executes on a threadpool thread.

Have a look here for more on delegates:
http://pobox.com/~skeet/csharp/events.html
 
Jeff said:
thanks, I understand that now

However there are something here I don't understand. I'm trying to
understand some .NET code using delegates. Below is another example which I
have trouble understanding. The example is taken from an app I'm programming
on (names on methods are changed):

Something mystifies me in this code:-
delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

public int World(string name)
{
IAsyncResult aResult;
HelloWorldHandler handler = delegate(string name) {return decimal;};
aResult = handler.BeginInvoke(name, null, null, null);

Where does the BeginInvoke method come from? The base Delegate abstract
type doesn't have it.
if (!aResult.IsComplete)
aResult.AsyncWaitHandle.WaitOne();

decimal var1 =
((HelloWorldHandler)((AsyncResult)aResult).AsyncDelegate).EndInvoke(aResult)
;

Ditto for EndInvoke?
}

There are lot of things I don't know here. Here no HelloWorldHandler foo =
HelloWorld; are used.. how can it still know what method to execute??

AFIAK the example above creates a delegate which execute in it's own thread.
started by BeginInvoke. and aResult.IsComplete check if the method reference
by the delegate is finished. And EndInvoke is called to retrieve the result
from the method.

Am missing something fundemental and just having an off day?
 
Where does the BeginInvoke method come from? The base Delegate abstract
type doesn't have it.

No - and it couldn't do, given that each delegate type would have its
own signature.

The compiler creates Invoke, BeginInvoke and EndInvoke methods for each
delegate type.

<snip>
 
Back
Top