Fog Creek Software
Discussion Board

NETFX 2.0 Anonymous Methods

Ok, I agree they're great for a couple things:
- For me, their greatest asset (right now) is FINALLY having an easy/safe way for your worker thread to "get back on the UI thread" and 'mess with' a control created on that thread. This is as fine as sliced bread! My progress controls as easier than ever . . . ;)
- Less Important: Quick/Brainless threading without defining delegate/thread-method.
- Even Less important (now getting a little scary): "inline" methods for trivial delegates/event-handling.

With the last one, now I'm a little frightened . . . ok, very, very frightened. This is really a GREAT tool for the informed, but for the uninformed (me?), can't it cause really asinine spaghetti code? Right now, it's fairly straightforward to find out what happens when I click my button, but if I utilize anonymous methods . . . "what happens" when I click my button could be anywhere and on any thread. A simple example, but you get my drift?

I guess it's nothing new, though, just another tool that should be use wisely . . . but, like data bound controls, it's a diamond studded shovel to dig your own gravel. Whatcha you guys/gals think?

Thursday, May 27, 2004

I wish I could do inline methods now in C#, just like in C++.

It makes good sense, form a design point of view, to separate code into functions, but for little two-liners it hardly seems worthwhile.

It would be nicer to call a two-line function, but it is very tempting to just copy those two lines instead. From a performance point of view, the overhead of calling such a function is not good, especially within a tight loop.

Getting inline functions back would address these issues nicely. Also, as it is a method decoration, it would only be used by explicitly declaring it, so is less "dangerous" to the uninitiated than some of the other options available.

Steve Jones (UK)
Thursday, May 27, 2004

Interestingly, according to Eric Gunnerson's blog:

"For C#, inlining happens at the JIT level, and the JIT generally makes a decent decision."

Just goes to show, you learn something new every day. Perhaps I should be writing my two-liner functions after all.

Steve Jones (UK)
Thursday, May 27, 2004

Does C# have functions within functions?

i like i
Thursday, May 27, 2004

No, but C# 2.0 will have lambda functions. That is, wherever a method delegate (~method pointer) is expected you can write an inline code block that acts as a function, with optional parameters.

You can also store the whole thing in a variable and pass the variable to other methods which will then execute this little function.

Chris Nahr
Thursday, May 27, 2004

*  Recent Topics

*  Fog Creek Home