473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Making sure 2.0 doesn't have blocks

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...
Nov 23 '05 #1
9 1014
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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...

Nov 23 '05 #2
Ah, but C# has always had delegates. If I understand you correctly, a
Block is just an anonymous method, assigned to a delegate.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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...

Nov 23 '05 #3
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 ); };
Nicholas Paldino [.NET/C# MVP] wrote:
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.

Nov 23 '05 #4
> 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?

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Nov 23 '05 #5
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...

"Lloyd Dupont" wrote:
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?


Nov 23 '05 #6
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).

"James Curran" wrote:
Ah, but C# has always had delegates. If I understand you correctly, a
Block is just an anonymous method, assigned to a delegate.


Nov 23 '05 #7
Brad <Br**@discussions.microsoft.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 23 '05 #8
Jon Skeet [C# MVP] wrote:
Brad <Br**@discussions.microsoft.com> wrote:
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.


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:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 24 '05 #9
You're exactly right; that's the way to go.

"Lasse Vågsæther Karlsen" wrote:
Jon Skeet [C# MVP] wrote:
Brad <Br**@discussions.microsoft.com> wrote:
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.


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:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Nov 28 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sandman | last post by:
I want to do this: function square ($a){ print "-- $a --"; } And use it like this: <? square( ?> Hello! <? ) ?> And get this:
31
by: CYBER | last post by:
Hello Is there any other way under python to create blocks ?? instead of def sth(x): return x
0
by: Michael Chermside | last post by:
Tim Williams writes: > I have to put my 2 cents in here. I love Python, but the one thing I > miss is using {} or something to enclose blocks. I edit in emacs > python-mode, and believe in...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
14
by: Haines Brown | last post by:
I know I'm missing something obvious. I need a short horizontal rule to preceed a line of text (in a bibliography in which the author is repeated). I tried this: <p> <div class="rule"></div>,...
1
by: yawnmoth | last post by:
I have two tables on the following page: http://www.frostjedi.com/terra/scripts/demo/tables.html I'm trying to make the top one look like the bottom in using only CSS. In FireFox, what I...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.