delegates and references to objects

Status
Not open for further replies.

BobLewiston

In Runtime
Messages
182
The concept of delegates (references to methods) per se is new to me (although I used function pointers in C years ago), and although the literature acknowledges that there are significant differences between delegates and the use of pointers to objects, I'm nonetheless a little confused by how similar the syntax used for references to methods is to that used for references to objects.

Recently at a few different forums I posted a brief outline of my understanding of the instantiation of objects, and got corroboration and helpful feedback from several developers. Let me very briefly summarize my current understanding, for comparison to the way delegates work:

Steps to create and prepare to use an object:

1 Define a class: create a "blueprint" for objects of this class.

2 Declare an object: create a reference variable to hold a reference (address) of an object. The reference variable must be of the same class as the object.

3 Instantiate an object: allocate space for the object in the heap.

4 Assign an object to an object reference variable: place an object's address in an object reference variable.


Syntax used to implement these steps (numerals refer to the steps given immediately above):

1 Define a class:
Code:
class <class name> { < whatever > }
2 Declare an object:
Code:
<class name> <object name>;
3 Instantiate an object:
Code:
new <class name> ();
This is usually done within the same statement as assigning the object to an object reference variable (see immediately below), unless the object's reference (address) is being returned to some calling method.

3 & 4 Instantiate an object and assign it to an object variable within a single statement:
Code:
<object name> = new <class name> ();
2, 3 & 4 Declare an object, instantiate it and assign it to an object variable within a single statement:
Code:
<class name> <object name> = new <class name> ();


This seems very similar to how I understand delegates to work, which I've summarized below.

Steps to create and prepare to use a delegate reference:

1 Define a delegate type: specify the signature (including return type) of methods that delegates of this type can point to. This is called "declaring" the delegate; which I think is kind of confusing, since "declaring an object" means to create a reference variable to hold the reference (address) of an object (see step #2 above for objects), but in my opinion "declaring" the delegate is really much more analogous to defining a class (see step #1 above for objects).

2 Declare a delegate reference variable: create a delegate reference variable to hold the reference (address) of some methods. The delegate reference variable must be of the delegate type with the same method signature as the methods it will point to.

3 Instantiate a delegate: allocate space for a delegate in the heap. [BTW, what actually goes in the heap here - the reference (address) of a method, or a copy of the method?]

4 Assign the delegate to a delegate reference variable: place a method's address in a delegate reference variable.

Syntax used to implement these steps (numerals refer to the steps given immediately above):

1 "Declare" a delegate:
Code:
delegate <return type> <delegate identifier> (<parameter list>);
2 Declare a delegate reference variable:
Code:
<delegate identifier> <delegate name>;
3 Instantiate a delegate:
Code:
new <delegate identifier> (<method name>);
3 & 4 Instantiate a delegate and assign it to a delegate reference variable within a single statement:
Code:
<delegate name> = new <delegate identifier> (<method name>);
2, 3 & 4 Declare a delegate, instantiate it and assign it to a delegate reference variable within a single statement:
Code:
<delegate identifier> <delegate name> = new <delegate identifier> (<method name>);

Any feedback on my thoughts would be appreciated.
 
this link may bevery helpful

C Sharp Programming/Delegates and Events - Wikibooks, collection of open-content textbooks

Events and delegates are fundamental to any Windows or Web Application. These allow the developer to "subscribe" to particular actions carried out by the user. Therefore instead of expecting everything and filtering out what you want, you choose what you want to be notified of and react to that action.

A delegate is a way of telling C# which method to call when an event is triggered. For example, if you click a Button on a form, the program would call a specific method. It is this pointer which is a delegate. Delegates are good because you can notify several methods that an event has occurred, if you so wish.

...

Delegates are a construct for abstracting and creating objects that reference methods and can be used to call those methods. Delegates form the basis of event handling in C#. A delegate declaration specifies a particular method signature. References to one or more methods can be added to a delegate instance. The delegate instance can then be "called" which effectively calls all the methods that have been added to the delegate instance.
 
3 Instantiate a delegate: allocate space for a delegate in the heap. [BTW, what actually goes in the heap here - the reference (address) of a method, or a copy of the method?]
A delegate is a reference object itself. It maintains a reference to the method it points to as an IntPtr.

You seem to understand the basics. However, you don't technically need to use the 'delegate' key word to create a delegate. It's another example of syntactic sugar, but it is quite common. In fact, I highly doubt you'd ever see any code that doesn't use that shortcut because it is very concise. Instead, you can use the static methods on the Delegate class to acheive the same result. For a beginner, it may be easier to take that approach. It's actually what the compiler is doing for you under the covers.
 
Status
Not open for further replies.
Back
Top Bottom