Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > General Computer Support > A delegate in .Net is juts like a function pointer in C#.

Reply
Thread Tools

A delegate in .Net is juts like a function pointer in C#.

 
 
VTS VTS is offline
Junior Member
Join Date: Nov 2010
Posts: 1
 
      11-10-2010
The most important property of a function pointer is that the syntax of the function and the pointer to the function must exactly be same. The returntypes and the list of arguments must match exactly.

Eg:

If a function looks like: int fact(int)
Then its function pointer should look like: int *p (int)

Here, as you may see the return types and the list of arguments of both function and function pointer match exactly.

When we use delegate then there are 3 stages which we need to follow:

Declaration: delegate <returntype> delegateName (<list of arguments>)
Object Creation: delegateName objDelegate = new delegateName(<function to which the delegate points>)
Invoking: objDelegate(<list of arguments that are to be passed to the function>)

The main uses of delegate comes in the following circumstances:

1) When we want to create the controls dynamically, then we cannot create the events for these controls beforehand as these controls are created only on runtime. In that case, if we want to bind these controls with their respective events then we need to use delegate.

So, delegate is first and foremost used for dynamic binding of controls with their events.

2) Delegate is also used in multitasking using threading.

Eg: Suppose we are copying a file from C: drive to D: drive. Then a progress bar would run which would indicate from time to time that how much contents have been copied - like 20% copied, 30% copied. This thing is an example of multiswitching, since here the thread is switching between 2 seperate opertaions. Once it is copying the data from one location to another location and then it is evaluating that how much content has been copied and increasing the progress bar accordingly. But, this switching is happening so fast, that the user gets a feeling as if 2 operations are happenng simultaneously.

So, in the above case, we may use a thread and make it switch between 2 operations. In this case also the delegate comes into play.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple inheritance/interface delegate through template function not working roman.blackhammer@gmail.com C++ 7 07-03-2007 09:00 AM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
Referencing web service complex data type within a second web service (like a delegate) Mike Dearman ASP .Net Web Services 1 06-23-2004 03:11 PM
passing a delegate to a C function. James ASP .Net 2 04-30-2004 01:30 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57