这是什么C#code以"箭头QUOT;意思是,它是怎么叫? [英] What does this C# code with an "arrow" mean and how is it called?
问题描述
我想在我的C#的客户端程序启用SSL,发现如下code <一个href="http://stackoverflow.com/questions/1742938/wcf-could-not-establish-trust-relationship-for-the-ssl-tls-secure-channel-with/1742967#1742967">in这个答案:
System.Net.ServicePointManager.ServerCertificateValidationCallback + =
(SE,证书,链,sslerror)=&GT;
{
返回true;
};
我增加了code到我的程序,它解决了这个问题,但我完全不明白究竟怎么了它的工作原理。
左侧部分 System.Net.ServicePointManager.ServerCertificateValidationCallback
一些回调 + =
修改了回调。但什么是剩余的结构是什么意思?我花了20分钟寻找到至少发现它是如何正确地调用,并在那里我可以找到如何读取更多的信息,但一切都是徒劳。我想这是主题相关的LINQ和搜索LINQ箭头,但没有发现任何合理的。
这是怎么回事(胡说,胡说,胡说)=&GT; {返回true;}
结构打来电话,我在哪里可以找到这种结构的详细信息。
这是一个lambda EX pression。这是一个非常特殊的匿名委托。基本上,你要定义一个方法,而不是给一个名字。它的参数是左边的 =&GT;
和方法体是右侧的 =&GT;
。在特定的情况下,
(SE,证书,链,sslerror)=&GT; {返回true; };
是一个lambda EX pression定义一个匿名方法。这种特殊的方法有四个参数
对象本身
x509证书证书
X509Chain链
SslPolicyErrors sslerror
和方法体为
返回true;
这是因为如果你说的
类ServerCertificateValidation {
公共BOOL OnRemoteCertificateValidation(
对象本身,
X509证书证书,
X509Chain链,
SslPolicyErrors sslerror
){
返回true;
}
}
然后
VAR验证=新ServerCertificateValidation();
System.Net.ServicePointManager.ServerCertificateValidationCallback + =
validation.OnRemoteCertificateValidation;
这是怎么回事
(胡说,胡说,胡说)=&GT; {返回true;}
结构打来电话,我在哪里可以找到这种结构的详细信息。
这就是所谓的以相同的方式,任何其它方法被调用。例如,你可以这样做:
Func键&LT; INT,INT,INT&GT;加法器=(M,N)=&GT; M + N;
在这里,我定义一个吃一对 INT
,并返回一个方法的 INT
。 INT 那
INT 4 =加法器(2,2);
下面是对拉姆达EX pressions ,并在一篇文章拉姆达运营商。如果你真的有兴趣,这个名字来自演算。
I was trying to enable SSL in my C# client program and found the following code in this answer:
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
(se, cert, chain, sslerror) =>
{
return true;
};
I added the code to my program and it solved the problem, but I completely don't get how exactly it works.
The left part System.Net.ServicePointManager.ServerCertificateValidationCallback
is some callback and +=
modifies that callback. But what does the remaining construct mean? I spent 20 minutes searching to at least find how it is properly called and where I can find more info on how to read that, but all in vain. I suppose it is somehow related to LINQ and searched for "LINQ arrow", but didn't find anything reasonable.
How is that (blah,blah,blah)=>{return true;}
construct called and where can I find more info on such constructs?
That is a lambda expression. It is a very special anonymous delegate. Basically you are defining a method and not giving a name. Its parameters are to the left of the =>
and the method body is to the right of the =>
. In your particular case,
(se, cert, chain, sslerror) => { return true; };
is an anonymous method defined by a lambda expression. This particular method has four parameters
object se
X509Certificate cert
X509Chain chain
SslPolicyErrors sslerror
and the method body is
return true;
It's as if you had said
class ServerCertificateValidation {
public bool OnRemoteCertificateValidation(
object se,
X509Certificate cert,
X509Chain chain,
SslPolicyErrors sslerror
) {
return true;
}
}
and then
var validation = new ServerCertificateValidation();
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
validation.OnRemoteCertificateValidation;
How is that
(blah,blah,blah)=>{return true;}
construct called and where can I find more info on such constructs?
It's called the same way that any other method is called. For example, you can do this:
Func<int, int, int> adder = (m, n) => m + n;
Here I am defining a method that eats a pair of int
and returns an int
. That int
is obtained by adding the values of the input parameters. It can be invoked like any other method.
int four = adder(2, 2);
Here's an article on MSDN on lambda expressions and an article on the lambda operator. If you're really interested, the name comes from lambda calculus.
这篇关于这是什么C#code以&QUOT;箭头QUOT;意思是,它是怎么叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!