开发者

Useful operators not yet implemented

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. :)


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


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜