in Technology by

What are delegates in C# and the uses of delegates?

1 Answer

0 votes
by
A Delegate is an abstraction of one or more function pointers (as existed in C++; the explanation about this is out of the scope of this article). The .NET has implemented the concept of function pointers in the form of delegates. With delegates, you can treat a function as data. Delegates allow functions to be passed as parameters, returned from a function as a value and stored in an array. Delegates have the following characteristics:
  • Delegates are derived from the System.MulticastDelegate class.
  • They have a signature and a return type. A function that is added to delegates must be compatible with this signature.
  • Delegates can point to either static or instance methods.
  • Once a delegate object has been created, it may dynamically invoke the methods it points to at runtime.
  • Delegates can call methods synchronously and asynchronously.
The delegate contains a couple of useful fields. The first one holds a reference to an object, and the second holds a method pointer. When you invoke the delegate, the instance method is called on the contained reference. However, if the object reference is null then the runtime understands this to mean that the method is a static method. Moreover, invoking a delegate syntactically is the exact same as calling a regular function. Therefore, delegates are perfect for implementing callbacks.

Related questions

0 votes
    is it possible to call c# methods written in managed code (maybe in a class or a library) from a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Which design pattern provides a single class which provides simplified methods required by client and delegates call to ... of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I'm encountering some problems in integrating segue and protocols while implementing a selection list. In my ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Why Do We Need Delegates?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    What are the .sca files and what are the uses?...
asked Sep 23, 2021 in Technology by JackTerrance
0 votes
    What are the uses of Amazon Glacier? Select the option from below list : a) access infrequent data ... AWS Interview Questions and Answers Agile Interview Questions Answers...
asked Jul 21, 2021 in Technology by Editorial Staff
0 votes
0 votes
    What are the uses of NumPy?...
asked Apr 24, 2021 in Technology by JackTerrance
0 votes
    What are the uses of Hibernate Framework?...
asked Feb 4, 2021 in Technology by JackTerrance
0 votes
    What are the uses of EventEmitter in Node.js?...
asked Jan 25, 2021 in Technology by JackTerrance
0 votes
    What are the uses of the keyword static?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What are the uses of Marker interface?...
asked Dec 21, 2020 in Technology by JackTerrance
+1 vote
    Which of the following are methods Traffic Manager uses to pick endpoints?...
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    What are the uses of Start, Duration and Delay options? tom Show Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
...