为什么我不能做?? =在C#中? [英] Why can't I do ??= in C#?

查看:89
本文介绍了为什么我不能做?? =在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己做的:

 富= foo的? X; 



为什么我不能做的:

 富?? = X; 



修改:我知道这不是语言的一部分...我问题是为什么不?我觉得有必要重复富是令人不快的和潜在的容易出错。它看起来就像丑陋的:

 富= foo的+ X; 


解决方案

在一般情况下,C#语言的设计团队是保守添加新的语法。他们说,一个除了语言的值必须针对增加的复杂性的语言的成本进行权衡。



彼得哈勒姆是C#编译器开发主管一会儿,也是C#语言的设计团队中的一员。他写了关于他测量的一个新功能的方式,叫做标尺,虽然他更专注于交互式GUI功能比语言特性。



这文字就是 ?? = 的问题,特别是相关的:




通常情况下,正确的设计决策是
不会改变任何东西。这大概是
中的最重要的教训我学到
从C#
语言设计的会议看安德斯。通常情况下,语言的
区在
其中有apppears是这可能是做了一些真正的
的收益上升。我们可以
防止用户从一个共同的编码
错误,或者我们可以改善
usabilityof一定
问题域的语言。那么对于解决这一问题的思维
真的很辛苦,通过所有的
选项之后,
答案尽....什么!即使
虽然问题是有效的,而且
看起来我们应该能够添加
的东西,以改善这种状况,
经过仔细思考,没有
方式为解决这的确
问题不是成本高于收益的解决方案更多。
很多我见过安德斯
进行的决定不是
复杂性添加到语言,除非
增值的最重要和
宝贵的决定证明了复杂性。
在模棱两可的情况,他的allmost
传真的动产没有什么比增加
的东西是有限的。




参考文献:




I often find myself doing:

foo = foo ?? x;

Why can't I do:

foo ??= x;

Edit: I know it's not part of the language... My question is "why not"? I find the necessity to repeat "foo" to be unpleasing and potentially error-prone. It looks just as ugly as:

foo = foo + x;

解决方案

In general, the C# language design team is conservative about adding new syntax. They say that the value of an addition to the language must be weighed against the cost of increased complexity in the language.

Peter Hallam was the C# Compiler development lead for a while, and also a member of the C# language design team. He wrote about his way of measuring a new feature, called the Yardstick, although he was focused more on interactive GUI features than language features.

This text is particularly relevant to the question of ??=:

Often the right design decision is to not change anything. This is probably the most important lesson I've learned from watching Anders in the C# language design meetings. Often an area of the language is raised in which there apppears to be some real gains which could be made. We could prevent the user from a common coding error, or we could improve the usabilityof the language for a certain problem domain. Then after thinking really hard, through all of the options for addressing the issue, the answer is to do .... nothing! Even though the problem is valid, and it looks like we should be able to add something to improve the situation, after careful reflection there is no way to address the issue which does not cost more than the solution gains. Many of the most important and valuable decisions I've seen Anders make are decisions to not add complexity to the language unless the added value justifies the complexity. In borderline cases, he allmost allways choses nothing over adding something which is marginal.

References:

这篇关于为什么我不能做?? =在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆