这个带有“箭头"的 C# 代码是什么意思?意思是怎么称呼? [英] What does this C# code with an "arrow" mean and how is it called?

查看:97
本文介绍了这个带有“箭头"的 C# 代码是什么意思?意思是怎么称呼?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 C# 客户端程序中启用 SSL 并发现以下代码 在这个答案中:

System.Net.ServicePointManager.ServerCertificateValidationCallback +=(se, 证书, 链, sslerror) =>{返回真;};

我将代码添加到我的程序中,它解决了问题,但我完全不明白它到底是如何工作的.

左侧部分 System.Net.ServicePointManager.ServerCertificateValidationCallback 是一些回调,+= 修改该回调.但是剩下的构造是什么意思呢?我花了 20 分钟搜索至少找到它是如何正确调用的,以及在哪里可以找到有关如何阅读它的更多信息,但都是徒劳的.我想它与 LINQ 有某种关系并搜索了LINQ 箭头",但没有找到任何合理的东西.

(blah,blah,blah)=>{return true;} 构造是如何调用的,我在哪里可以找到有关此类构造的更多信息?

解决方案

那是一个 lambda 表达式.这是一个非常特殊的匿名 代表.基本上你是在定义一个方法而不是给出一个名字.它的参数在 => 的左边,方法体在 => 的右边.在您的特定情况下,

(se, cert, chain, sslerror) =>{ 返回真;};

是由 lambda 表达式定义的匿名方法.这个特殊的方法有四个参数

对象序列X509证书证书X509链条链条SslPolicyErrors sslerror

方法体是

返回真;

就好像你说过

class ServerCertificateValidation {public bool OnRemoteCertificateValidation(对象本身,X509证书证书,X509链条链条,SslPolicyErrors sslerror){返回真;}}

然后

var validation = new ServerCertificateValidation();System.Net.ServicePointManager.ServerCertificateValidationCallback +=验证.OnRemoteCertificateValidation;

<块引用>

(blah,blah,blah)=>{return true;} 构造是如何调用的,我在哪里可以找到有关此类构造的更多信息?

它的调用方式与调用任何其他方法的方式相同.例如,您可以这样做:

Func加法器 = (m, n) =>m + n;

这里我定义了一个方法,它吃掉一对 int 并返回一个 int.int 是通过将输入参数的值相加而获得的.它可以像任何其他方法一样被调用.

int 四 = adder(2, 2);

这是 MSDN 上关于 lambda 表达式的文章和关于lambda 运算符.如果你真的感兴趣,这个名字来自 lambda calculus.

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# 代码是什么意思?意思是怎么称呼?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆