this.Foo(); but I don’t know what is β€œs”...">

What is "s" and "e" in C # code syntax

I see the following code:

textBox.TextChanged += (s, e) => this.Foo();

but I don’t know what is β€œs” and β€œe”? what should i learn in c # for this line of code?

+5
source share
7 answers

They are the parameters of the lambda function.

The compiler takes its types out of context, but it is allowed to write a longer (more informative) form:

 textBox.TextChanged += (object s, EventArgs e) => { this.Foo(); };

In these notations it is easier to see that they are the parameters of the method.
On the other hand =>is the body of the method.


In response to the comment:

Now is there a way to rewrite the same lambda expression that I have in the simpler C # syntax?

, . "" "", , .

// The setup method
void MyMethod()
{
   //textBox.TextChanged += new Eventhandler(MyTextChangedHandler);  // C#1 and later
   textBox.TextChanged += MyTextChangedHandler;                      // C#2 and later
}

// The subscribed method. The lambda is an inline version of this. 
private void MyTextChangedHandler(object s, EventArgs e)
{ 
   this.Foo(); 
}
+14

s - , e - , EventArgs EventArgs. lambda expression

+4

lamda, . TextChanded , , object sender, EventArgs e. - , s=sender, e=eventargs. , :

textBox.TextChanged += new EventHandler(delegate (Object s, EventArgs e) {

            });
+2

, , lambdas.

s e - , =>.

+1

S (, ), e . stackoverflow :

lambdas

+1

. :

protected void TextBox_TextChanged(Object Sender, EventArgs Args)
+1

-. (, , ).

private void TextBox_TextChanged(object sender, EventArgs e)
{
    this.Foo();
}

,

textBox.TextChanged += TextBox_TextChanged;

textBox.TextChanged += (s, e) => this.Foo();

textBox.TextChanged += (object s, EventArgs e) => this.Foo();

#. s e TextBox_TextChanged: object sender, EventArgs e. , =>.

textBox.TextChanged += (s, e) => this.Foo(s, e);

(if Foo has an appropriate list of parameters) The name of these parameters does not matter.


UPDATE

If the required method (or delegate) requires a return value, you can leave the keyword returnin a lambda expression. Given this method

public void WriteValues(Func<double, double> f)
{
    for (int i = 0; i <= 10; i++) {
        Console.WriteLine("f({0}) = {1}", i, f(i));
    }
}

you can make these calls

WriteValues(x => x * x);
WriteValues(x => Math.Sin(x));
WriteValues(t => Math.Exp(-t));
+1
source

All Articles