📜  c# func vs action - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:06.290000             🧑  作者: Mango

代码示例1
//A Action is a container to set a method for invoking that has no returns 
//where as a Func is a container to set a method for invoking that has returns
//so
//Any Generals or Templates in a Action will always be signiture input params
//Where as to say a function will alway have a output return indicated by the
//last General or Template type and any others will be inputs params.
//Addtionally async invokers (.BeginInvoke) with actions can be fire and forget 
//where as a Funcs must be awaited for the result using the EndInvoke
//example using lambda's (Short handed anonymous methods):
//Actions
Action BasicAction = ()=>{/*does something*/};
Action SingleParamiterizedAction = (x)=>{/*does something*/};
Action DoubleParamiterizedAction = (x, y)=>{/*does something*/};
//....
Func BasicFunc = ()=>{/*does something*/ return TheTResult; };
Func SingleParamiterizedFunc = (x)=>{/*does something*/ return TheTypeResult; };
Func DoubleParamiterizedFunc = (x, y)=>{/*does something*/ return TheTypeResult; }
//Usage example
BasicAction.Invoke(); //result of void can not be used
TResult TheResult = BasicFunc(); // result of the methods output can be used.