Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

.net - What does this C# code with an "arrow" mean and how is it called?

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?

question from:https://stackoverflow.com/questions/4829054/what-does-this-c-sharp-code-with-an-arrow-mean-and-how-is-it-called

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...