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>
精彩评论