开发者

Func and lambda question [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I have a declaration of

void Test (Func<bool> f)

I have 开发者_运维技巧a method like bool getItem(string id)

I can call like Test ( ()=>getItem("123")), why?

I suppose that I can check I need 1 string parameter.


Func<bool> expects a function that returns a bool. Func<T1> has one output argument, then Func<T1, T2> takes a function with input T1 and output T2. Each successive version allows an additional input argument, with the final generic type being the type of your output argument.

ex.: Func<string, string, bool> would be able to invoke bool DoStuff(string s1, string s2)

Quick Edit to clarify: Test( () => getItem("123")) works because the start of your lambda declaration exposes no input arguments, and getItem returns a boolean.


because the signature of Test requires the argument be a method that takes no parameters and returns a boolean.


Your method Test is defined as taking a parameter of Func<bool> which expects a method signature similar to bool Something();

Look at the other Func<> options to see which match what you're trying to accomplish. At the very least you're looking at either Action<string> or Func<bool, string>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜