The Action delegate type in C# represents a method that can be called, but as its return type is void, it does not produce a value. It can take in one or more parameters as input.
Action exists in the System namespace and can be accessed in multiple overloads to support varying numbers of parameters. It's commonly used to create event handlers, callbacks, and functions to pass as arguments.
- No Parameters: An Action can be defined as a method that does not accept any parameters and returns no value.
Key Features
- Absence of Return Value: In contrast to Func<T>, the Action delegate does not yield a return value; it consistently returns void.
- Capability for Multiple Parameters: Action<T1, T2, ..., T16> allows for the definition of a delegate that can accept as many as 16 parameters.
- Utilization of Anonymous Methods and Lambda Expressions: Action is frequently employed alongside lambda expressions or anonymous methods, promoting more succinct code.
- Application in Event Handlers: Action proves beneficial when it is necessary to transfer methods (including parameters), particularly for callbacks or event-handling scenarios.
Comparison with Func
- Action<T>: Used when the method returns void and can take zero or more parameters.
- Func<T, TResult>: Used when the method returns a value (TResult) and can take zero or more input parameters.
For example
- Func<int, int, int> could represent a method that takes two integers and returns an integer (like an addition function).
- Action<int, int> could represent a method that takes two integers and performs an action (like printing the sum), but doesn't return a value.
- Action is a delegate type used for methods that don't return values.
- It is useful for passing methods as parameters or defining callbacks.
- You can use Action for methods with zero to 16 input parameters.
- Action with No Parameters
Step 1. I have developed a WPF user interface, as illustrated below. You may select any application type that aligns with your specific requirements.
MainWindow.Xaml code
MainWindow.Xaml.cs code
Step 2. Establish a class titled "ActionMethods.cs" as illustrated below. It has been used to define action delegates
Step 3. Create a class named “MessageHandler.cs” as shown below. It will be used to manage an event of action type.
Step 4. Create a class by the name of ‘EventHandlerManager.cs” to handle the event.
- RaiseOnEventRaised(): A static method that triggers the OnEventRaised event.
- Action onEventRaised = OnEventRaised: Creates a local copy of the event delegate. This is done to avoid the potential issues of the event being null while invoking it.
- if (onEventRaised != null): Checks if there are any subscribers to the event. If there are, it proceeds to invoke the event.
- onEventRaised(): Invokes the event. This calls all methods that have subscribed to OnEventRaised.
Step 6. The results obtained from testing are.
Action with No Parameters
Action with One Parameter
Action with Multiple Parameters
Passing Action as a Parameter to a Method(Callback)
Using Action with Event Handling
Windows Hosting Recommendation
0 comments:
Post a Comment