what is a c# delegate, again

This post is mostly about the BB case as defined in the post on 2 fundamental categories.

Let’s try this question again — not simple.

#1 A simple delegate instance holds a method’s address[1] + a Target object [2] to call it on.
** I feel the delegate concept gives more meaning to the traditional “callback” concept
** Argument to the method is passed at invocation time, not creation time. In this respect, delegate == functor.

#2 In practice, a delegate instance often holds a bunch [3] of single-method delegate (instances). Supports append/removeLast. If you program your delegate to be invoked at runtime, all those methods run sequentially by insertion order — key language feature.

Warning — in c# discussions, a “delegate” can mean a delegate Type or a delegate Instance. Always know what is meant in each case.

The Target can be null when we instantiate a delegate with a static method. In that case the full address {class, method} is remembered.

[1] an address, not a a pointer variable
[2]This target thingy is a permanent pointer
[3] actually a reverse singly linked list, known as the invocation list.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s