Home Code Action and Func Delegates in C# – Explained with Examples

Action and Func Delegates in C# – Explained with Examples

by TM

Delegates in C# are used to represent references to methods with specific signatures, allowing developers to implement callback methods, handle events, and perform tasks at a later time. Two commonly used delegates are Action and Func.

The Action delegate encapsulates a method with parameters that doesn’t return a value. It simplifies the process of passing a method as a parameter to another method. An example of using the Action delegate is shown in a console calculator application, where basic arithmetic operations are performed using the provided parameters.

The Func delegate, on the other hand, represents a method with parameters that returns a value. Unlike the Action delegate, the return type of a Func delegate can be any type. In the example provided, a Calculator class is defined, and Func delegates are created to point to the methods in the Calculator class. The results of the operations are then printed to the console.

In conclusion, Action and Func delegates provide a way to encapsulate method calls within delegate objects, allowing for easy utilization of methods as parameters and return values.

To read the full article and access the source code, visit the GitHub Repository.

Meta Data: {“keywords”:”delegates, Action delegate, Func delegate”}

Source link

You may also like

Leave a Comment

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Update Required Flash plugin