Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello all,
 
While reading about delegates, i stucked in 1 point that is "why we use delegates"??
 
I know delegates are used as a reference to function and used to call functions at run-time rather than compile-time.
 
But still i am not able to understand why exactly we use...
 
Is it increases speed??
 
Okay now i have a piece of code:
 
public delegate double Delegate_Prod(int a,int b);
class Class1
{
    static int fn_Prodvalues(int val1,int val2)
    {
        return val1*val2;
    }
 
    static void Main(string[] args)
    {
        Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
        double res = delObj(5,6);
        Console.WriteLine ("Result : ",+res);
        Console.ReadLine();
    }
}
 
But this can be done directly by calling class object also...
Posted 14-Oct-13 7:48am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Please see my past answers:
What is the Extra Advantage of Delegate[^],
What are the advantages of delegates in C#.NET?[^].
 
See also these two answers:
Confusion about delegates[^],
Pass a method with N params as parameter in C#[^].
 
[EDIT]
Atul Khanduri wrote:
Now I have another simple question or rather need a simple example for a function having delegates in their parameter.
Nothing silly about it, but I thought it would be better for you to write your own function first and ask a question if you stuck.
 
I remember I already wrote such examples. Look here: Pass a method with N params as parameter in C#[^] (I already referenced this answer above; the code sample is complete).
 
—SA
  Permalink  
v2
Comments
Atul Khanduri at 14-Oct-13 17:58pm
   
Awesome description in the 1st one(Extra advantage).
Saw it and gave my 5 points.
Now going to second.... :-)
Sergey Alexandrovich Kryukov at 14-Oct-13 18:22pm
   
Thank you, Atul. Hope those answers will explain many things, as well as my article "Dynamic Method Dispatcher" referenced in them; it has a section "On the Nature of Delegate Instances".
Please don't forget to accept this answer formally (green "Accept" button).
—SA
Atul Khanduri at 14-Oct-13 18:48pm
   
With delegates we can pass a function(delegate instance) as a argument to different function, which we can't do by using classic OOP's...
Am i right??
Sergey Alexandrovich Kryukov at 14-Oct-13 20:07pm
   
Absolutely. This is what I mean when calling delegate instances, according to the tradition formed in the theory of programming languages, "first-class citizen".
 
Note that it is not just the functions. As an OOP method can be an instance (non-static) method which pass "this" pointer as the (hidden, implicit) first argument, the value of this managed pointer, which points to the instance, is also stored in the delegate instance. Also, it does not have to be just one function. You can add more then one (via additional '+=' operation), and then the delegate instance becomes "multicast" one, which is implemented by having a member called InvocationList, which is a list of single-cast delegate instanced stored in a single multicast instance. All those calls are done automatically when you invoke (call Invoke) a multicast delegate instance.
 
If you read my article on "Dynamic Method Dispatcher", you will find another section, "6. Complement OOP or Abuse it?", showing some uses of delegates which can complement "classical" OOP.
 
—SA
Atul Khanduri at 15-Oct-13 4:50am
   
Thanks for the help sir....
Now i have another simple question or rather need a simple example for a function having delegates in their parameter.
I know its a silly question but can you please....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

When we make an object of a class then we can access all functions of a class and Base class functions too. Here we can see that a user can simple make an object of a class and access all information of class. But in real word this is drawback of an object.
 
For example we are an account holder of a bank, and we have permission to access all information, like other account holder’s details, bank deposited details and etc. think what happens if bank provide all accessibly to account user.
 
Through delegate we can provide limited accessibility to the user. It behaves run-time as a class and yes it can save time, support dynamic method invocation and break accessibility of all functions. It is similar of function pointer in c and it defines what that function looks like.Binding Events to Event Handlers is probably your first introduction to delegates.
 
To see live examples and know further uses of delegates i would like to refer you on these along with above links:
When would you use delegates in C#? [^]
Delegates, Why[^]
when & why to use delegates?[^]
Practical Use of Delegate in real time Application -1
[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 15 Oct 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100