views:

74

answers:

1

Based on this, how would you make ignoring parameters more succint?

var m = menuStrip.Items.Add("Hello", null, delegate { MessageBox.Show("Como Esta Mundo"); });

I'm thinking along the lines of:

var m = menuStrip.Items.Add("Hello", null, ==> MessageBox.Show("Como Esta Mundo") );

var m = menuStrip.Items.Add("Hello", null, ? => MessageBox.Show("Como Esta Mundo") );

or perhaps this:

var m = menuStrip.Items.Add("Hello", null, ?=> MessageBox.Show("Como Esta Mundo") );

What would be your take?

<language evolution observation> They even add ?? operator to the language, which is indeed beautiful. Now I think if they would like to make a converse of it (i.e. NullIf operator), this is very useful if you want to treat something as the same "" or zero is equal to null </language evolution observation>

+2  A: 

Well, just like anonymous methods, you'd have to know the compile-time type you were trying to convert the lambda expression to (unless C# starts supporting free functions, which is a bigger change).

I quite like ?=> though:

Action<int> foo = ?=> Console.WriteLine("Okay");
EventHandler handler = ?=> Console.WriteLine("Okay too");

Now as ToolStripCollection.Add specifically declares EventHandler as the delegate, your examples would be okay. This wouldn't though:

control.Invoke(?=> Console.WriteLine("No no no"));

Here the compiler wouldn't know which type to try to convert the lambda expression to, so it would require a cast - just like anonymous methods do today.

Jon Skeet