What is "s" and "e" in C # code syntax
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();
}
-. (, , ).
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));