Press "Enter" to skip to content

Author: Russell Finn

Followup to first annoying C# thing

My friend Nick dashed off the following quick reply to my previous screed (in its first incarnation as an email message to him):

I think it’s the difference between thinking of it as a verb or a noun.

That would be fine, except they’re using it as a noun. Allow me to quote from Dumbill and Bornstein:

These methods [such as OnFileSystemEvent, which will be referred to later] must match the delegates declared in FileSystemWatcher. From the Mono FileSystemEventHandler.cs […] source file […]:

public delegate void FileSystemEventHandler (object sender, FileSystemEventArgs e);

A delegate declaration defines an instance of a class descended from System.Delegate. In FileSystemWatcher, then, FileSystemEventHandler is an implicit subclass of System.Delegate.

So it looks like a function declaration, but it’s really a subclass declaration? (I presume “instance of a class” doesn’t mean “an object that belongs to a class” here.)

… Next, we construct a new delegate of type FileSystemEventHandler and use the += operator to add it to each of the FileSystemWatcher‘s FileSystemEventHandler events.

[The code that does this is:

FileSystemEventHandler onFileSystemEvent = new FileSystemEventHandler (OnFileSystemEvent);

where OnFileSystemEvent was previously declared as a static method with a signature matching the one above.]

So what we appear to have is a declaration that looks like a function but really declares a subclass of a special type of object that has only one method and no instance variables. Then you create one of these — things — initializing it with a matching method, and finally attach it to another object in a special way, such that it will be called back when a particular — something — happens.

I’m not saying this isn’t useful, but couldn’t they have presented it a different way, or called it something different (“callback”)?

Maybe I’m putting the blame in the wrong place. The philosophy of the Developer’s Notebook series seems to be to get you writing code quickly, and eschew all the theory. (They even title this section “Define Function Pointers”.) Perhaps I should crack open Hejlsberg et al. and see if they explain it more precisely.

Comments closed

The (first) thing annoying about C#

[I haven’t gotten too far into the language so I can’t presume there won’t be more…]

I want to like C#. I think it has a lot of advantages over the widely used languages that come before it. When it was first released, it was widely believed to be a replacement for Java; but I’m looking at it more as a replacement for C++ — the little things that are peculiar idioms or complex constructs in C++ seem to tend to be directly supported in C#. So I’m predisposed to like it.

But then I come across these annoying Microsoft-isms — specifically, the Humpty-Dumpty-like [1] way in which they define ordinary computing terms like “events” and “delegates” to be something almost, but not quite, completely different.

I know what a “delegate” is — it’s an object that handles behavior for another object. A common design pattern, and one supported pretty much directly in Objective-C and Cocoa. But that’s not what a C# delegate is, apparently — although I’m not sure exactly what it’s supposed to be. It’s “like a function pointer”, except we don’t have those in C#. And the first example I see (in Mono: A Developer’s Handbook) suggests that the thing you declare as a delegate is really a prototype for the action that the actual thing you instantiate is supposed to take — huh? Wouldn’t that be the delegate?

And apparently an “event” is a collection of delegates, and not, well, an event.

And somehow delegates are magic so you can instantiate one, add it to an “event”, then later instantiate a different one and remove it from the same event, and the “right thing” happens. (Because they’re “like function pointers”.)

Maybe this will make sense if I keep reading. (I hesitate to crack open The C# Programming Language, which is also on my desk; I’m a bit of a programming language nut, but this one looks like I need a law degree to get through it.)

[1] I could have said “Orwellian”; aren’t you proud of me?

Comments closed