Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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:

C#
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
Comments
Member 12452061-Vishal 13-Sep-16 10:45am    
Good article
Member 12452061-Vishal 13-Sep-16 10:47am    
Need some more details like delegates can be static, delegates can be used with lambda expression or not.

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
 
Share this answer
 
v2
Comments
Atul Khanduri 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 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 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 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 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....
See this tutorial[^], and this one[^].
 
Share this answer
 
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
[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900