in Education by

Why Do We Need Delegates?

1 Answer

0 votes
by
Historically, the Windows API made frequent use of C-style function pointers to create callback functions. Using a callback, programmers were able to configure one function to report back to another function in the application. So the objective of using a callback is to handle button-clicking, menu-selection, and mouse-moving activities. But the problem with this traditional approach is that the callback functions were not type-safe. In the .NET framework, callbacks are still possible using delegates with a more efficient approach. Delegates maintain three important pieces of information:
  • The parameters of the method.
  • The address of the method it calls.
  • The return type of the method.
A delegate is a solution for situations in which you want to pass methods around to other methods. You are so accustomed to passing data to methods as parameters that the idea of passing methods as an argument instead of data might sound a little strange. However, there are cases in which you have a method that does something, for instance, invoking some other method. You do not know at compile time what this second method is. That information is available only at runtime, hence Delegates are the device to overcome such complications.

Related questions

0 votes
    Why Should We Use WCF Service?...
asked Apr 1, 2021 in Education by JackTerrance
0 votes
    Explain the need of display mode in MVC?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
0 votes
    What is WPF Dependency Property and how can we use?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    How can we improve the Performance of an ASP.NET Web Page?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
    How can we apply themes in ASP.NET application?...
asked Apr 6, 2021 in Education by JackTerrance
0 votes
    Why do we need biological neural networks? (a) to solve tasks like machine vision & natural language processing (b) ... (d) all of the mentioned Please answer the above question....
asked Sep 21, 2022 in Education by JackTerrance
0 votes
    Why do we need political parties ? Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    I am new to Hadoop/ZooKeeper. I cannot understand the purpose of using ZooKeeper with Hadoop, is ZooKeeper writing ... with Hadoop? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I am new in software testing and looking to enhance my knowledge. I want to know in detail about: What is ... answer here. Thanks! Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me why do we need Ethical Hacking? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    why do we need language ? explain the types of language used in java? answer fast plz also not in very short Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    Why do we need to collect data? Select the correct answer from above options...
asked Nov 12, 2021 in Education by JackTerrance
...