Making sure 2.0 doesn't have blocks

  • Thread starter Thread starter Brad Wood
  • Start date Start date
B

Brad Wood

I know 2.0 supports "anonymous methods" (or "closures"), but I don't see
the ability to assign a block of code to a variable (I think they are
simply "blocks" in Ruby).

Am I correct? They seem logically to go hand in hand...
 
Brad,

In order to do this, you would have to assign it to a delegate. You
would do so like this:

// Create a delegate pointing to the block of code.
EventHandler handler =
delegate(object sender, EventArgs e)
{
// Your code here.
};

I believe that if you don't have to access the parameters, you don't
need the delegate keyword, and can just assign the block, but I would double
check.

Hope this helps.
 
Yes, a regular 1.1 delegate does seem work as a block (has access to
variables within current scope) in 2.0, so this:

string dude;
EventHandler handler = delegate( object sender, EventArgs e )
{ Console.WriteLine( dude ); };
dude = "one";
handler( null, null );
dude = "two";
handler( null, null );

does report:
one
two

Now I can't figure out how why this won't compile...

delegate myBlock = delegate { Console.WriteLine( dude ); };
 
Now I can't figure out how why this won't compile...
delegate myBlock = delegate { Console.WriteLine( dude ); };
Because this one is not typed.
Which kind of delegate is it?
 
That's just it; I don't want a "kind" of delegate, I just want a block of
code, so I suppose I'm just trying to stretch the language beyond where I
should.

A sort of workaround would be a simple delegate type that represents a
method with no parameters. Does such an animal exist? I don't know how to
search the documentation for a list of all delegate types...
 
After reading up a bit, as I understand it, in the Lisp/Ruby tradition, a
block is simply a chunk of code kind of like a C macro that can be assigned
to a variable. A closure goes a step further and adds binding to variables
within it's scope even if the object that declared the closure and variables
is no longer in scope.

It appears to me that 2.0 delegates can still act like 1x delegates but can
also be used almost as pure closures; the difference being that you must have
a previously declared delegate type to reference (more strongly typed).
 
Brad said:
That's just it; I don't want a "kind" of delegate, I just want a block of
code, so I suppose I'm just trying to stretch the language beyond where I
should.

A sort of workaround would be a simple delegate type that represents a
method with no parameters. Does such an animal exist? I don't know how to
search the documentation for a list of all delegate types...

ThreadStart would be an obvious example.
 
Jon said:
ThreadStart would be an obvious example.

I defined two delegates in my class library, AnonymousMethod (takes no
parameters, returns nothing), and AnonymousParameterizedMethod (takes
object as parameter, returns object), just so the meaning is more clear.
No actual difference between ThreadStart and AnonymousMethod but my code
looks cleaner and I avoid the questions I got when I used ThreadStart:
"Hey, where's the thread that is going to execute this threading code?".
 
You're exactly right; that's the way to go.

Lasse Vågsæther Karlsen said:
I defined two delegates in my class library, AnonymousMethod (takes no
parameters, returns nothing), and AnonymousParameterizedMethod (takes
object as parameter, returns object), just so the meaning is more clear.
No actual difference between ThreadStart and AnonymousMethod but my code
looks cleaner and I avoid the questions I got when I used ThreadStart:
"Hey, where's the thread that is going to execute this threading code?".

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:[email protected]
PGP KeyID: 0x2A42A1C2
 
Back
Top