none
Delegates RRS feed

  • Question

  • Hi,

        Can anyone please explain about delegates i.e 

         why we use delegates

         and When we use this delegates in realtime projects

         and what is the use of it

    thanks in advance

    Wednesday, January 16, 2013 7:04 AM

Answers

  • Refer below links which nicely explain everything about delegates.

    http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx

    http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/C-Sharp-delegates/

    http://www.csharp-station.com/tutorials/lesson14.aspx

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 7:19 AM
  • What is delegate?

    A delegate is a method reference (Function pointer) - it allows you to set up a method to be called at a later date. When you call the method, you do not need to know what it is called - you just execute it from the delegate.

    Why do we use them?

    They can be faster. They can make methods more generic: a delegate with can be loaded with as Console or TextBox write method can be called throughout a method, or class, and the same code handles both functions.

    Delegates play a key role in Event handling. Imagine a textbox that you place on the UI. Now at runtime, when you press a key on the button, you have about 3-4 events that execute - KeyUp, KeyDown, GotFocus etc. Attaching the right handlers would allow you to handle only those actions that are important to you.


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:16 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 10:03 AM
  • Hi Kumar J,

    Delegate is a special type of object in .NET that can hold a reference to some specified type of method.
    Using that delegate object you can call the encapsulated method, pass that delegate object to some other methods or return as a function return object to some other function.

    Many times we need to pass logic (instead of data) to any method, in that case delegate objects are used. You can see live examples in maximum LINQ methods.

    Delegates are used in Event construct in .NET.

    Visit MSDN library for more help.


    One good question is equivalent to ten best answers.



    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:16 AM
    • Edited by Shyam Kr Wednesday, January 16, 2013 10:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 10:14 AM
  • Here is a very good explanation:

    http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Jason Dot Wang Thursday, January 17, 2013 8:15 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 11:58 AM

All replies

  • @why we use delegates

    When you only know that you wanna a result, but you don't know how to get the result; or the result may come from different kinds of algorithms with the same parameters in order, number as well as types.

    @When we use this delegates in realtime projects

    Lambda Expressions, some methods like List.FindAll needs your own delegate called "Predicate".

    @what is the use of it

    Example:

    List<int> numbers = new List<int>();

    numbers.FindAll(num=>Your condition that returns a boolean value);


    帮助一起改进论坛质量?提交你的意见于此。
    我的博客园
    慈善点击,点击此处
    和谐拯救危机,全集下载,净化人心

    Wednesday, January 16, 2013 7:16 AM
  • Refer below links which nicely explain everything about delegates.

    http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx

    http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/C-Sharp-delegates/

    http://www.csharp-station.com/tutorials/lesson14.aspx

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 7:19 AM
  • What is delegate?

    A delegate is a method reference (Function pointer) - it allows you to set up a method to be called at a later date. When you call the method, you do not need to know what it is called - you just execute it from the delegate.

    Why do we use them?

    They can be faster. They can make methods more generic: a delegate with can be loaded with as Console or TextBox write method can be called throughout a method, or class, and the same code handles both functions.

    Delegates play a key role in Event handling. Imagine a textbox that you place on the UI. Now at runtime, when you press a key on the button, you have about 3-4 events that execute - KeyUp, KeyDown, GotFocus etc. Attaching the right handlers would allow you to handle only those actions that are important to you.


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:16 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 10:03 AM
  • Hi Kumar J,

    Delegate is a special type of object in .NET that can hold a reference to some specified type of method.
    Using that delegate object you can call the encapsulated method, pass that delegate object to some other methods or return as a function return object to some other function.

    Many times we need to pass logic (instead of data) to any method, in that case delegate objects are used. You can see live examples in maximum LINQ methods.

    Delegates are used in Event construct in .NET.

    Visit MSDN library for more help.


    One good question is equivalent to ten best answers.



    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:16 AM
    • Edited by Shyam Kr Wednesday, January 16, 2013 10:17 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 10:14 AM
  • Here is a very good explanation:

    http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Jason Dot Wang Thursday, January 17, 2013 8:15 AM
    • Marked as answer by Jason Dot Wang Wednesday, January 23, 2013 9:06 AM
    Wednesday, January 16, 2013 11:58 AM
  • Thanks to all for reply....

    Sambath Raj.C

    Imagine a textbox that you place on the UI. Now at runtime, when you press a key on the button, you have about 3-4 events that execute - KeyUp, KeyDown, GotFocus etc. Attaching the right handlers would allow you to handle only those actions that are important to you.

    can you please give me some example with sample code.... 

    waiting for your reply.....


    Wednesday, January 16, 2013 1:38 PM
  • Hi,
    when you press the button alongwith click event another events are also raised as you said.But if you need only click event to handle in your code then you will register a method as event handler like this :

    void SomeMethod()
    {
    Button button1 = new Button();
    button1.Click += new RoutedEventHandler(button1_Click);
    }
    
    void button1_Click(object sender, RoutedEventArgs e)
            {
                throw new NotImplementedException();
            }
    

    There are other ways also for attaching event handlers if you are using ASP.NET or Silverlight or WPF framework that is declaratively using markup.

    Does this makes sense to you or you need anything else.


    One good question is equivalent to ten best answers.

    Wednesday, January 16, 2013 1:48 PM
    • There is the definition of a Delegate which is this:  A declaration of a method signature.
    • Then there is the assignment of a Delegate which points to an actual method of same signature.
    • Since the creation of the new Action class, Actions are delegates and easier to use.
    //The old way : defines what a method should look like
    delegate void mydelgate(string parmeter);
    //this method matches the delgate above
    private void myMethod (String parameter){}
    //It matches because it returns void and takes in a string
    
    //the new way
    public Action<string> ActionA = MyAction;
    //MyAction below will be called whenever ActionA is called
    private void MyAction(String){}

    You say; well what good is this? Why don't I just call the method directly...

    Answer: By making the method that implement the action/delegate private you are hiding it from outsiders.  This is good practice. 

    So as said earlier, delegates and actions are method pointers, they allow you pass references to methods to anywhere you want.

    Is there more? Yes...

    Delegates/Actions and Func (Functions) are the basis for events.  A little difficult to understand at first but an event is a signal that something happened.  Like a Click Event on a button.  Therefore when the event is signalled someone has to be "listening" to process it.  The listeners are methods, that are registered to receive the event.  The only way to work with methods as pointers is with delegates.  We often see how to wire up event handlers which do what?  They simply point to the method to handle the event!

    TextBox.Keydown += MyKeyDownHandler;
    public void MyKeyDownHandler(Object Sender, KeyEventArgs e){
       //now you know who sent it and you have the key event arugments 
    }
    


    JP Cowboy Coders Unite!

    Wednesday, January 16, 2013 1:53 PM