All of the Func delegates return a value. What are the .NET delegates that can be used with methods that return void?
delegate: It is the keyword which is used to define the delegate. return_type: It is the type of value returned by the methods which the delegate will be going to call. It can be void. A method must have the same return type as the delegate.
When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.
Anonymous function converted to a void returning delegate cannot return a value.
string GetMessage() { return "Hello there!"; } Func<string> sayHello = GetMessage; Console. WriteLine(sayHello()); In the example, we use the Func delegate which has no parameters and returns a single value. This is the function to which we refer with the help of the Func delegate.
All Func delegates return something; all the Action delegates return void.
Func<TResult>
takes no arguments and returns TResult:
public delegate TResult Func<TResult>()
Action<T>
takes one argument and does not return a value:
public delegate void Action<T>(T obj)
Action
is the simplest, 'bare' delegate:
public delegate void Action()
There's also Func<TArg1, TResult>
and Action<TArg1, TArg2>
(and others up to 16 arguments). All of these (except for Action<T>
) are new to .NET 3.5 (defined in System.Core).
... takes no arguments and has a void return type?
I believe Action
is a solution to this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With