473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

delegates and references to objects

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:
Expand|Select|Wrap|Line Numbers
  1. class <class name> { < whatever > }
2 Declare an object:
Expand|Select|Wrap|Line Numbers
  1. <class name> <object name>;
3 Instantiate an object:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. <object name> = new <class name> ();
2, 3 & 4 Declare an object, instantiate it and assign it to an object variable within a single statement:
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1. delegate <return type> <delegate identifier> (<parameter list>);
2 Declare a delegate reference variable:
Expand|Select|Wrap|Line Numbers
  1. <delegate identifier> <delegate name>;
3 Instantiate a delegate:
Expand|Select|Wrap|Line Numbers
  1. new <delegate identifier> (<method name>);
3 & 4 Instantiate a delegate and assign it to a delegate reference variable within a single statement:
Expand|Select|Wrap|Line Numbers
  1. <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:
Expand|Select|Wrap|Line Numbers
  1. <delegate identifier> <delegate name> = new <delegate identifier> (<method name>);
Any feedback on my thoughts would be appreciated.
Feb 3 '09 #1
2 1860
r035198x
13,262 8TB
1 There is no loss of generality here. Remember that even for objects, defining a class and declaring a class are not different. It's only declaring and initializing objects of those classes that is different.
4.) I would think that a copy is made.
Feb 3 '09 #2
vekipeki
229 Expert 100+
Your custom delegates are actually classes derived from the Delegate class. The only difference is that you cannot inherit Delegate class explicitly, but compiler does that for you.

So, when you create a new delegate, a new instance of your derived delegate is created, which contains a reference to the actual method (and object instance, if method is not static). This object is instantiated in heap, as any other object.

Check this link also: http://msdn.microsoft.com/en-us/libr....delegate.aspx
Feb 3 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
1
by: James Smyth | last post by:
I currently have a .NET Form application which has a seperate thread invoking delegates of the MainForm in order to have the main thread update the form's controls. I would like to move the...
15
by: Rhy Mednick | last post by:
I have a class (let's call it ClassA) that I've written which has events. In another class (let's call it ClassB) I create a collection of ClassA objects. In a third class (ClassC) I create a...
6
by: Andrew Hayes | last post by:
Having to remember to unregister for events to prevent a ref count seems to be quite a burden to place on a developer. I have reviewed the WeakMulticastDelegate solution proposed by Xavier Musy...
15
by: Marshal | last post by:
First... let's deal with Delegates. Comments welcome. 1) Invoking a NULL delegate is annoying. ** (It should just do nothing rather than crash.) 2) It's too easy to accidently attach multiple...
0
by: James Smyth | last post by:
I currently have a .NET Form application which has a seperate thread invoking delegates of the MainForm in order to have the main thread update the form's controls. I would like to move the...
14
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
2
by: Matthew Herrmann | last post by:
Hi, I've heard from groups that listeners to event handlers cause references to be kept alive, if the targets are marked to stay alive. I need to make sure that attaching events to objects will...
13
by: heddy | last post by:
Using Jesse Liberty's excellent book on C#, I am looking at chapter 12 - Delegates. Now, I was under the impression that a delegate is essentially a pointer to a member function. However, when I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.