Func delegate with no return type
All of the Func delegates return a value. What are the .NET delegates that can be used with methods that return void?
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.
All of the Func delegates take at least one parameter
That's not true. They all take at least one type argument, but that argument determines the return type.
So Func<T>
accepts no parameters and returns a value. Use Action
or Action<T>
when you don't want to return a value.
上一篇: 使用匿名代理进行事件处理
下一篇: Func委托没有返回类型