Delegates Interview Questions

Collection of typically Interview Questions on delegates for interview preparation. you can also read more about delegates, callbacks, and Multicast Delegates. Alternatively, you can also test your knowledge by an online free quiz for delegates and multicast delegate.

Interview question: What is a delegate?

Answer: A delegate is a type-safe function pointer. Using delegates you can pass methods as parameters. To pass a method as a parameter, to a delegate, the signature of the method must match the signature of the delegate. This is why delegates are called type-safe function pointers


Interview Question: What is the main use of delegates in C#?

Answer: Delegates are mainly used to define callback methods and for event declarations.


Interview Question: Why callback methods use delegates?

Answer: This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. For example, a sort algorithm could be passed a reference to the method that compares two objects. Separating the comparison code allows the algorithm to be written in a more general way.


Interview Question: What is asynchronous callback ?

Answer: Delegate types are sealed—they cannot be derived from— and it is not possible to derive custom classes from Delegate. Because the instantiated delegate is an object, it can be passed as a parameter, or assigned to a property. This allows a method to accept a delegate as a parameter, and call the delegate at some later time. This is known as an asynchronous callback and is a common method of notifying a caller when a long process has completed. When a delegate is used in this fashion, the code using the delegate does not need any knowledge of the implementation of the method being used. The functionality is similar to the encapsulation interfaces provide.


Interview Question: When to Use Delegates Instead of Interfaces?

Answer: Both delegates and interfaces enable a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct. A delegate can be created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object that has no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should it use an interface?

Use a delegate in the following circumstances:

An eventing design pattern is used.

It is desirable to encapsulate a static method.

The caller has no need to access other properties, methods, or interfaces on the object implementing the method.

Easy composition is desired.

A class may need more than one implementation of the method.

Use an interface in the following circumstances:

There is a group of related methods that may be called.

A class only needs one implementation of the method.

The class using the interface will want to cast that interface to other interface or class types.

The method being implemented is linked to the type or identity of the class: for example, comparison methods.


Interview Question: What are the different ways of creating a delegate in C# ?

Answer:

  • Action
  • Func
  • Predict
  • Lambda
  • Anonymous types

Interview Question: Explain about BeginInvoke method of delegates?

Answer: The BeginInvoke method initiates the asynchronous call. It has the same parameters as the method that you want to execute asynchronously, plus two additional optional parameters. The first parameter is an AsyncCallback delegate that references a method to be called when the asynchronous call completes. A second parameter is a user-defined object that passes information into the callback method. BeginInvoke returns immediately and does not wait for the asynchronous call to complete. BeginInvoke returns an IAsyncResult, which can be used to monitor the progress of the asynchronous call.


Interview Question: Explain about EndInvoke method?

Answer:

The EndInvoke method retrieves the results of the asynchronous call. It can be called any time after BeginInvoke. If the asynchronous call has not completed, EndInvoke blocks the calling thread until it completes. The parameters of EndInvoke include the out and ref parameters

of the method that you want to execute asynchronously, plus the IAsyncResult returned by BeginInvoke.


An online free quiz about delegates, you can also read more about delegates, callbacks and Multicast Delegates

Leave a Reply

Your email address will not be published. Required fields are marked *