我的代码中有一个 Func 声明如下:
Func<string, int, bool> Filter { get; set; }
如何获取作为 Func 参数的字符串和 int 变量,以便在我的代码中使用它们?
最佳答案
参数仅在调用函数时存在...并且它们仅在函数内可用。例如:
foo.Filter = (text, length) => text.Length > length;
bool longer = foo.Filter("yes this is long", 5);
这里,值“yes this is long”是 text
参数的值在委托(delegate)执行时,同样值 5 是 length
参数 在执行时。在其他时候,这是一个毫无意义的概念。
您真正想要达到的目标是什么?如果您能为我们提供更多背景信息,我们几乎肯定能为您提供更好的帮助。
关于c# - 使用 Func<T> 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11067087/