5

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?

Bohn
  • 26,091
  • 61
  • 167
  • 254
  • Just a variable name for Source and EventArgs – Gonzalo.- Sep 21 '12 at 15:00
  • Just to add to the answers given, the reason they are standing on their own without type identifiers is because the compiler can infer their usage from the event handler, so it's unnecessary to explicitly state `(object sender, TextChangedEventArgs e)` (not sure on the types, just guessing) – Chris Sinclair Sep 21 '12 at 15:00

7 Answers7

14

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(); 
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • thanks, so the keyword to Google for me is to learn "Lambda Expression"...Now is there a way to also rewrite the same lambda expression I have in a simpler C# syntax? well without Lambda expressions. – Bohn Sep 21 '12 at 15:03
  • @BDotA "simpler" is subjective. Most people consider lambdas to be the simplest way of doing this. – Servy Sep 21 '12 at 15:05
  • @Servy - Yes, but when learning this it's easier to separate eventhandlers and lambdas for a while. BdotA: see edit. – H H Sep 21 '12 at 15:06
4

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

Adil
  • 146,340
  • 25
  • 209
  • 204
2

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

            });
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
1

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

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
1

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

Community
  • 1
  • 1
iivel
  • 2,576
  • 22
  • 19
1

parameters of method. for example :

protected void TextBox_TextChanged(Object Sender, EventArgs Args)
Davecz
  • 1,199
  • 3
  • 19
  • 43
1

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));
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188