[英]Func<> with lambda expression
I have found following part of code in some examples while learning Func<> syntax: 在学习Func <>语法时,我在一些示例中找到了以下部分代码:
public static class Lambda
{
public static int MyFunc(Func<string, int> func)
{
//some logic
return 0;
}
}
And sample call : 样品来电:
var getInt = Lambda.MyFunc((url) => { Console.WriteLine(url); return 0; }
And My Question : 而我的问题:
Why passing above func as lambda expression with this (url) is allowed if value is never assigned ( or maybe is ?)? 如果从未赋值(或者可能是?),为什么允许将带有this(url)的lambda表达式传递给func以上? What is the point of passing Func like this ? 像这样传递Func有什么意义?
Edit : To clarify my question . 编辑:澄清我的问题。 I was only wondering about this sample call - why passing string as argument like above (using lambda (url) => {} ) is not forbidden by compiler if the value can not be initiated. 我只是想知道这个示例调用 - 如果无法启动值,编译器不禁止像上面那样传递字符串(使用lambda(url)=> {})。 Is there any example that can be useful with passing string like above ? 有没有任何例子可以用于传递像上面的字符串?
url
is the name of the parameter for the lambda expression. url
是lambda表达式的参数名称。 It's like writing a method like this: 这就像写一个这样的方法:
public static int Foo(string url)
{
Console.WriteLine(url);
return 0;
}
Then creating a delegate from it: 然后从中创建委托:
Func<string, int> func = Foo;
Now in order to call the delegate, you need to provide it a string - and that then becomes the value of the parameter, just like if you called the method normally: 现在,为了调用委托,您需要提供一个字符串 - 然后成为参数的值,就像您正常调用该方法一样:
int result = func("some url");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.