开发者

()=>getItem("123") is Func(bool) or Func(string,bool)

开发者 https://www.devze.com 2023-01-25 11:44 出处:网络
For lambda expresssion ()=>getItem(\"123\"), is it Func(bool开发者_运维问答) or Func(string,bool), suppose getItem return bool.It\'s a Func<bool>.

For lambda expresssion ()=>getItem("123"), is it Func(bool开发者_运维问答) or Func(string,bool), suppose getItem return bool.


It's a Func<bool>.

The clue is in the () => part: This means that the function has no input parameters.


If you want to refactor this to a Func<string,bool> then pull the literal "123" out and treat it as an input parameter instead:

bool getItem(string input) { ... }

Func<bool> selector = () => getItem("123");
Func<string,bool> selector2= str => getItem(str);     

bool result1 = selector();
bool result2 = selector2("123");

Assert.AreEqual(result1,result2);


Strictly speaking, the answer is neither. Lambda expressions are typeless

0

精彩评论

暂无评论...
验证码 换一张
取 消