views:

195

answers:

2

Reading from MSDN: "A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method."

Does then "delegate" mean a type or an object?!

...It cannot be both. It seems to me that the single word is used in two different meanings:

  1. a type containing a reference to a method of some specified signature,
  2. an object of that type, which can be actually called like a method.

I would prefer a more precise vocabulary and use "delegate type" for the first case. I have been recently reading a lot about events and delegates and that ambiguity was making me confused many times.

Some other uses of "delegate" word in MSDN in the first meaning:

  • "Custom event delegates are needed only when an event generates event data"
  • "A delegate declaration defines a class that is derived from the class System.Delegate"

Some other uses of "delegate" word in MSDN in the second meaning:

  • "specify a delegate that will be called upon the occurrence of some event"
  • "Delegates are objects that refer to methods. They are sometimes described as type-safe function pointers"

What do you think? Why did people from Microsoft introduced this ambiguity? Am I the only person to have conceptual problems with different notions being referenced with the same word.

+9  A: 

The text is using the word delegate both ways; as a type and as an instance of the type.

It should say "A delegate type is a type that references a method. Once a delegate instance is assigned a method, it behaves exactly like that method."

John Saunders
+1 Nicely done.
Andrew Hare
Note that the `delegate` keyword in C# is just as ambiguous: `delegate xyz Xyz()` indicates a delegate *type*, `delegate() { ... }` indicates a delegate *instance*.
Ruben
A: 

A delegate type refers to a signature, not a specific method. A delegate instance, may, if assigned, contain one or more methods references.

So the delegate type is:

delegate void D();

The delegate instance is:

D d;

The delegate instance assignment may be:

d += new D (Method);
Marcello Belguardi