如果您是一名语言设计师,您将如何使 lambda 忽略参数更加简洁?
基于这个,如何使忽略参数更加简洁?
var m = menuStrip.Items.Add("Hello", null, delegate { MessageBox.Show("Como Esta Mundo"); });
我正在思考:
var m = menuStrip.Items.Add("Hello", null, ==> MessageBox.Show("Como Esta Mundo") );
var m = menuStrip.Items.Add("Hello", null, ? => MessageBox.Show("Como Esta Mundo") );
或者可能是这样:
var m = menuStrip.Items.Add("Hello", null, ?=> MessageBox.Show("Como Esta Mundo") );
你会怎么看?
<语言演化观察> 他们甚至添加?? 运算符到语言中,这确实很漂亮。 现在我想如果他们想与之相反(即NullIf运算符),如果你想将某些东西视为相同的“”或零等于null,这是非常有用的。
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,就像匿名方法一样,您必须知道尝试将 lambda 表达式转换为的编译时类型(除非 C# 开始支持自由函数,这是一个更大的变化)。
我很喜欢?=> 不过:
现在,由于
ToolStripCollection.Add
专门将EventHandler
声明为委托,因此您的示例就可以了。 但事实并非如此:这里编译器不知道尝试将 lambda 表达式转换为哪种类型,因此需要进行强制转换 - 就像今天的匿名方法一样。
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:
Now as
ToolStripCollection.Add
specifically declaresEventHandler
as the delegate, your examples would be okay. This wouldn't though: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.