有用的运算符尚未实现

发布于 2024-09-30 11:42:03 字数 1055 浏览 2 评论 0原文

C# 中是否有 but 运算符的空间?

即:

if (checkBox.Checked but comboBox.SelectedItem == null) { ... }

相对于

if (checkBox.Checked == false || (checkBox.Checked && comboBox.SelectedItem != null)) { ... }

哪些其他当前不存在的运算符可能有助于节省键入可读性较差的语句?

更新
KennyTM 发布了一个优秀替代方案我建议使用 but 运算符,但它有点没有抓住重点。我知道现有的运算符可以组合起来创建任何可能的表达式,我只是想知道我的代码如何更好地表达我的意图。

“如果未选中复选框或未选择任何内容” 不会扫描完全以及“如果选中复选框但未选择任何内容”< /em>.

更新2:
只是对那些说“and”与“but”相同的人进行更新。虽然我同意逻辑上这是正确的(并且我不会重新打开这个问题),但我想与您分享一句话,表明“和”和“但是”之间的选择有时会产生很大的不同:

在我们的客户食堂吃了咖喱午餐。它看起来有点像猫病但是它非常好吃。
Phil Winstanley - 2011 年 2 月 22 日

非常感谢。 :)

Is there room in C# for a but operator in C#?

i.e.:

if (checkBox.Checked but comboBox.SelectedItem == null) { ... }

as opposed to

if (checkBox.Checked == false || (checkBox.Checked && comboBox.SelectedItem != null)) { ... }

What other currently non-existent operators could be useful to save typing out a less readable statement?

UPDATE
KennyTM has posted an excellent alternative to the but operator I suggested, but it's kind've missing the point. I know the existing operators can be combined to create any expression possible, I'm just wondering how better my code might express my intent.

I.e. "if the checkbox is not checked or nothing is selected" doesn't scan quite as well as "if the checkbox is checked but nothing is selected".

UPDATE 2:
Just an update for those who say that "and" is the same as "but". While I agree that logically that is correct (and I'm not re-opening this issue) I wanted to a share a quote with you that suggests the choice between "and" and "but" can sometimes make a big difference:

Had a curry for lunch in our customers canteen. It looked a bit like cat sick but it was quite tasty.
Phil Winstanley - 22nd Feb 2011

Many thanks. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绝不放开 2024-10-07 11:42:03
(a but b)  ==  (!a || (a && !b))
           ==  ((!a || a) && (!a || !b))
           ==  (!a || !b)

因此

if (!checkBox.Checked || comboBox.SelectedItem != null) { ... }

我发现这并不比创建新运算符的可读性差多少。此外,andornot 在布尔变量的编程语言中具有明确的含义,但 but 不是t,所以你需要浪费一行文档来解释 (a but b) == (!a || !b),而不是 a && !ba && b给大家。这不值得。

(a but b)  ==  (!a || (a && !b))
           ==  ((!a || a) && (!a || !b))
           ==  (!a || !b)

hence

if (!checkBox.Checked || comboBox.SelectedItem != null) { ... }

I find that this isn't much less readable than creating a new operator. Moreover, and, or, not have well-establish meanings in programming languages for Boolean variables, but but isn't, so you'll need a waste a line of documentation explaining (a but b) == (!a || !b), instead of a && !b or a && b to everyone. It doesn't worth it.

欢你一世 2024-10-07 11:42:03

是什么阻止您编写 But 扩展方法?

public static bool But(this bool original, bool secondExpression)
{
  return original || (original && secondExpression));

}

在我看来,这更好,因为它促进编程进入一种语言,而不是仅仅使用一种语言。

What stops you from writing a But Extension method?

public static bool But(this bool original, bool secondExpression)
{
  return original || (original && secondExpression));

}

This is better in my opinion as it promotes programming INTO a language opposed to just using one.

幸福还没到 2024-10-07 11:42:03

我认为“但是”与“并且”的意思相同!例如
“我喜欢 C# 但我讨厌 C++”与“我喜欢 C# 但我讨厌 C++”的意思是一样的。它们都是连词(连接语句)。

在您的示例中,您肯定只想在选中组合框但没有选定项目的情况下执行 if 语句的主体吗?这意味着要归档,必须选中组合框; IE:
checkBox.Checked && 因此,

我认为您的“但是”应该翻译成的示例是不正确的。

我的 C# 很生疏,但如果有一种方法可以在 C# 中定义类似宏的东西,您可以将“but”定义为“&&”的别名,但我认为这不会产生什么好处。

I think that "but" means the same as "and"! E.g.
"I like C# BUT I hate C++" means the same thing as "I like C# AND I hate C++". Both of them are conjunctions (joining statements).

In your example, surely you only want the body of the if statement to be executed in a situation where the combobox is checked but there is not selected item? This implies that for it to file, the combobox must be checked; i.e.:
checkBox.Checked && comboBox.SelectedItem == null

Therefore, I think your example of what 'but' should translate to is incorrect.

My C# is rusty, but if there is a way of defining something like macros in C#, you could define "but" as an alias for "&&", but I think it would yield very little benefit.

地狱即天堂 2024-10-07 11:42:03

如果我们从这里开始,它会在哪里结束。然而,如果你喜欢这样的东西,也许 Boo 适合你。这允许您添加自己的语法。

If we start here, where does it end. However, if you like such things, maybe Boo is for you. This allows you to add your own syntax.

回忆凄美了谁 2024-10-07 11:42:03

在 Perl 中,你可以这样写:

if ($checkBox.Checked) doSomething() unless (!$comboBox.SelectedItem);

就我个人而言,我非常赞成更具表现力的语言,但这必须与 TMTOWTDI

In Perl, you could write:

if ($checkBox.Checked) doSomething() unless (!$comboBox.SelectedItem);

Personally, I'm very much in favour of more expressive languages, but that has to be balanced against the pain caused by TMTOWTDI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文