I see some code like this:
textBox.TextChanged += (s, e) => this.Foo();
but I don't know what are "s" and "e" ? what is the topic I should study in C# for this line of code?
They are the parameters of the lambda-function.
The compiler infers their types from the context, but it is allowed to write a longer (more informative) form:
textBox.TextChanged += (object s, EventArgs e) => { this.Foo(); };
In this notation it is easier to see that they are method parameters.
On the other side of =>
is the method body.
In response to comment:
Now is there a way to also rewrite the same lambda expression I have in a simpler C# syntax?
Yes, you can always use the classic notation. And while that may not be 'better' or even 'simpler' it is easier to understand when you are learning this.
// 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();
}
s
is source object and e
is object that should be EventArgs
object or extended from EventArgs class, You have to read lambda expression
Is a lamda expression used as a shortcut for a delegate.
TextChanded expect a delegate taking two arguments, object sender, EventArgs e
. The lambda version mark these arguments with placeholders, that is s=sender, e=eventargs
. Is just a syntactic sugar since behind the scene it converts to:
textBox.TextChanged += new EventHandler(delegate (Object s, EventArgs e) {
});
The topic you are looking for is called expression lambdas.
s
and e
are names of parameters of an anonymous function whose body is indicated after the =>
.
S is usually going to be the sender (object that invoked the event) and e will be the event arguments. This stackoverflow answer should explain it:
Weak event handler model for use with lambdas
parameters of method. for example :
protected void TextBox_TextChanged(Object Sender, EventArgs Args)
This is a lambda expression. A lambda expression is a very concise way of writing a method (or a delegate to be precise). You could write a method like this
private void TextBox_TextChanged(object sender, EventArgs e)
{
this.Foo();
}
and then add the handler like this
textBox.TextChanged += TextBox_TextChanged;
it would be equivalent to
textBox.TextChanged += (s, e) => this.Foo();
You could also write
textBox.TextChanged += (object s, EventArgs e) => this.Foo();
but the types of the arguments are inferred automatically by the C# compiler. s
and e
are the paramters and are equivalent to the paramters of the TextBox_TextChanged
method: object sender, EventArgs e
. They could be used in the expression following the =>
.
textBox.TextChanged += (s, e) => this.Foo(s, e);
(assuming Foo has a corresponding parameter list) The name of these parameters doesn't matter.
UPDATE:
If the required method (or delegate) requires a return value, you can drop the return
keyword in the 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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With