Generic Delegates (Action, Func, Predicate) – Interview Questions

Collection of typically Interview Questions on generic delegates for interview preparation. you can also read more about delegatescallbacksMulticast Delegates, Generic Delegates. Alternatively, you can also test your knowledge by an online free quiz for delegatesmulticast delegate, generic delegates.

Interview Question: What are Generic Delegates in C#?

Answer: The Func , Action and Predicate are the three generic delegates. We can make use of all these three predefined generic delegates when we want to invoke the methods without defining the delegates explicitly. The Generic Delegates in C# were introduced as part of .NET Framework 3.5.


Interview Question: Why do we need the Generic Delegates?

Answer: Generic delegates allow user to invoke the delegate without creating instance of that delegate, which helps you to maximize code reuse, type safety, and performance. You can create your own generic interfaces, classes, methods, events, and delegates.


Interview Question: What is difference between Action and Func?

Answer:

Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).


Interview Question: When do we need Func delegate?

Answer: Whenever delegate returns some value, whether by taking any input parameter or not, you need to use the Func delegate


Interview Question: When do we need Action delegate?

Answer: Whenever delegate does not return any value, whether by taking any input parameter or not, then you need to use the Action delegate.


Interview Question: When do we need Predicates delegate?

Answer: Whenever delegate returns a Boolean value, by taking one input parameter, then you need to use the Predicate delegate.


Interview Question: What will happen if our method’s return type is “bool” but it takes 2 parameters. How can we use the “Predicate” delegate?

Answer: you do not have to make use of the “Predicate” delegate, in this case, you will make use of the “Func” delegate, which will take 2 input parameters and one out parameter (of “bool” type).

you can also read more about delegatescallbacksMulticast Delegates, Generic Delegates. Alternatively, you can also test your knowledge by an online free quiz for delegatesmulticast delegate, generic delegates.

Leave a Reply

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