locked
Why we need to use delegate RRS feed

  • Question

  • we know delegate is function pointer and with delegate we call function but i like to know without delegate we can call function. so why we need to use delegate.

    please show me few scenario where we need to use delegate to call function with example code.

    here a sample code where delegate is used to call function and passing data to function

    // Form1
    public delegate void delPassData(TextBox text);

    // Click Handler
    private void btnSend_Click(object sender, System.EventArgs e)
    {
         Form2 frm= new Form2();
         delPassData del=new delPassData(frm.funData);
         del(this.textBox1);
         frm.Show();
    }

    // Form2

    public void funData(TextBox txtForm1)
    {

         label1.Text = txtForm1.Text;
    }

    instead of delegate we can create instance of Form2 frm= new Form2(); and call Form2 function funData directly instead of delegate.....so what would be harm in this case if we would not use delegate.

    found one good example for when to use delegate http://haroldrv.com/2015/02/understanding-delegates-and-when-to-use-them-in-c-asp-net/

    https://softwareengineering.stackexchange.com/questions/211276/why-would-i-ever-use-delegates-if-im-not-doing-events

    thanks




    • Edited by Sudip_inn Wednesday, March 14, 2018 2:27 PM
    Wednesday, March 14, 2018 2:01 PM

Answers

  • Hello Sudip_inn,

    >>so what would be harm in this case if we would not use delegate.

    In my opinion, a delegate can be seen as a placeholder for a/some method(s), like declaring a variable and you could "assign" method to it. As for you situation, how would you record funData method in order to invoke later if you don't use delegate. And if you need to invoke a mount of method multi times, the good choice is to build delegate chain by "+=" operate.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, March 15, 2018 11:25 AM
    Thursday, March 15, 2018 8:09 AM
  • , the good choice is to build delegate chain by "+=" operate.

    Best regards,

    Neil Hu

    .

    Hi Fei,

    Can you explain why it is a good choice in this situation?.

    Not telling it is your opinion. One of the things an MVP must never do is giving his or her opinion but give facts. You are of course no MVP but that is as well for an MSFT.  

    A delegate should be used if there is in no other way a reference to an object or  member. However, the better answer was what Sam gave. 

    What you have to learn as MSFT is when people ask something as an acknowledgement what they think or as a real question. It is an easy way so score points by the way by just acknowledging what they ask you to tell. 


    Success
    Cor




    • Edited by Cor Ligthert Thursday, March 15, 2018 1:01 PM
    • Marked as answer by Sudip_inn Friday, March 16, 2018 1:25 PM
    Thursday, March 15, 2018 11:56 AM

All replies

  • If you don't have a use of a delegate then don't use one.


    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by Cor Ligthert Wednesday, March 14, 2018 3:01 PM
    Wednesday, March 14, 2018 2:47 PM
  • If you don't have a use of a delegate then don't use one.


    Sam Hobbs
    SimpleSamples.Info

    Yea in fact there is no better answer for this question (not the question header but the text). 

    Success
    Cor

    Wednesday, March 14, 2018 3:02 PM
  • Hello Sudip_inn,

    >>so what would be harm in this case if we would not use delegate.

    In my opinion, a delegate can be seen as a placeholder for a/some method(s), like declaring a variable and you could "assign" method to it. As for you situation, how would you record funData method in order to invoke later if you don't use delegate. And if you need to invoke a mount of method multi times, the good choice is to build delegate chain by "+=" operate.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, March 15, 2018 11:25 AM
    Thursday, March 15, 2018 8:09 AM
  • @Fei thanks for answer

    you said - how would you record funData method in order to invoke later if you don't use delegate. And if you need to invoke a mount of method multi times, the good choice is to build delegate chain by "+=" operate.

    but things not clear to me what you try to say here. please explain with example code which show clearly when to use delegate.

    Thursday, March 15, 2018 11:26 AM
  • , the good choice is to build delegate chain by "+=" operate.

    Best regards,

    Neil Hu

    .

    Hi Fei,

    Can you explain why it is a good choice in this situation?.

    Not telling it is your opinion. One of the things an MVP must never do is giving his or her opinion but give facts. You are of course no MVP but that is as well for an MSFT.  

    A delegate should be used if there is in no other way a reference to an object or  member. However, the better answer was what Sam gave. 

    What you have to learn as MSFT is when people ask something as an acknowledgement what they think or as a real question. It is an easy way so score points by the way by just acknowledging what they ask you to tell. 


    Success
    Cor




    • Edited by Cor Ligthert Thursday, March 15, 2018 1:01 PM
    • Marked as answer by Sudip_inn Friday, March 16, 2018 1:25 PM
    Thursday, March 15, 2018 11:56 AM
  • Yea in fact there is no better answer for this question (not the question header but the text). 

    We both like to help people solve problems they are having difficulty solving. Sometimes people ask questions that have plenty of answers elsewhere. The more that we answer those questions the more likely it is that they (and others) will ask more like that. In this case the person does not have a real problem to solve and presumes that delegates have no use and seems to be challenging us to prove they do. If the question had asked for examples of the use of delegates without the presumption that there are none then I might have tried to help with that.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, March 15, 2018 7:09 PM