• +91 9723535972
  • info@interviewmaterial.com

CSharp Interview Questions and Answers

Question - Give a detailed explanation of Delegates in C#.

Answer -

Delegates are variables that hold references to methods. It is a function pointer or reference type. Both the Delegate and the method to which it refers can have the same signature. All Delegates derives from the

System.Delegate namespace.
Following example demonstrates declaring a delegate:

public delegate AddNumbers(int n);
After declaring a delegate, the object must be created of the delegate using the new keyword, such as:

AddNumbers an1 = new AddNumbers(number);
The Delegate offers a kind of encapsulation to the reference method, which gets internally called with the calling of the delegate. In the following example, we have a delegate myDel that takes an integer value as a parameter: public delegate int myDel(int number); public class Program { public int AddNumbers(int a) { Int Sum = a + 10; return Sum; } public void Start() { myDel DelgateExample = AddNumbers; } }

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners