Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are "s" and "e" in C# code syntax

Tags:

c#

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?

like image 465
Bohn Avatar asked Sep 21 '12 14:09

Bohn


7 Answers

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(); 
}
like image 67
Henk Holterman Avatar answered Oct 03 '22 06:10

Henk Holterman


s is source object and e is object that should be EventArgs object or extended from EventArgs class, You have to read lambda expression

like image 39
Adil Avatar answered Oct 03 '22 04:10

Adil


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) {

            });
like image 43
Felice Pollano Avatar answered Oct 03 '22 05:10

Felice Pollano


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 =>.

like image 27
O. R. Mapper Avatar answered Oct 03 '22 06:10

O. R. Mapper


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

like image 26
iivel Avatar answered Oct 03 '22 05:10

iivel


parameters of method. for example :

protected void TextBox_TextChanged(Object Sender, EventArgs Args)
like image 41
Davecz Avatar answered Oct 03 '22 04:10

Davecz


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));
like image 38
Olivier Jacot-Descombes Avatar answered Oct 03 '22 06:10

Olivier Jacot-Descombes