C# 用于切换 bool 成员的更好的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12751194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nicer code for toggling a bool member
提问by
Please note that the question isn't about negating a boolean value but rather about the most elegant, efficient and nicest way of doing so.
请注意,问题不是关于否定布尔值,而是关于最优雅、最有效和最好的方法。
Upon a click on a toggle button, I execute the following code to store the new state in a private bool.
单击切换按钮后,我执行以下代码将新状态存储在私有bool.
_isIt = !_isIt;
It works, sure. It's readable, sure. But so is i = i + 1, still, we always write i++. Is there a neater way to toggle a bool?
它确实有效。它是可读的,当然。但也是如此i = i + 1,我们总是写i++。有没有更简洁的方法来切换 a bool?
采纳答案by Konrad Viltersten
No. That's the optimal way. Congratulations!
不,这是最佳方式。恭喜!
Even IFthere would be an other way (as you commented - _isIt!!), I believe that it would be better not to use it because you'd be raising a lot of eye browses anyway.
即使有另一种方式(正如您所评论的 - _isIt!!),我相信最好不要使用它,因为无论如何您都会引起很多眼睛浏览。
回答by driis
No, I believe that is the idiomatic C# way of toggling a bool(i.e. assigning it the opposite value).
不,我相信这是切换 a 的惯用 C# 方式bool(即为其分配相反的值)。
My opinion: Keep your code as it is. It clearly conveys the intent of the code, which is the most important thing for future readers.
我的意见:保持你的代码原样。它清楚地传达了代码的意图,这对未来的读者来说是最重要的。
If you are interested in alternativeways of doing it, you could use XOR, but I will argue it is less readable.
如果您对其他方法感兴趣,可以使用 XOR,但我认为它的可读性较差。
_isIt ^= true;
回答by Armen Tsirunyan
No, there isn't. I don't know what else to add to this answer :)
不,没有。我不知道还有什么要添加到这个答案:)
回答by gustavodidomenico
In fact there is another option to toggle a boolean value, this will work:
事实上,还有另一个选项可以切换布尔值,这将起作用:
_isIt ^= true;
However this is not better than the way you did...
然而,这并不比你做的方式更好......
回答by Emma
A Quick Note
快速说明
Though an answer has been accepted, I believe a more in-depth answer would be nice for future readers to have. As this post has pointed out, the !or NOToperator is the most widely accepted, and the optimal solution. In fact, when compared to ^or XORover 10,000 iterations it is roughly 100ms faster. The usage all depends on the circumstance to be honest. Either way, below you will find the three most common methods of toggling a boolvalue, and you should choose what you are most comfortable with using, within the standards you're allowed to use.
虽然答案已被接受,但我相信更深入的答案对未来的读者来说会很好。正如这篇文章所指出的,!orNOT运算符是最被广泛接受的,也是最佳解决方案。事实上,与10,000 次^或XOR超过 10,000 次迭代相比,它大约快 100 毫秒。用法都是视情况而定。无论哪种方式,您都会在下面找到三种最常用的切换bool值的方法,并且您应该在允许使用的标准范围内选择最适合使用的方法。
The if-elseMethod
该if-else方法
The easiest way to toggle a boolvalue is to write out an if-elsethat assigns the new value; this way even a complete beginner to development can understand what is going on. This is actually how toggling is taught in most conceptual classes.
切换bool值的最简单方法是写出if-else分配新值的 ;这样,即使是一个完整的开发初学者也可以理解发生了什么。这实际上是大多数概念课程中教授切换的方式。
bool b = true;
if (b == true)
b = false;
else
b = true;
Of course the ifcan be simplified to if (b), but traditionally, you'll write it all the way out for clarity and learning purposes.
当然if可以简化为if (b),但传统上,为了清晰和学习目的,您会一直写它。
The NOT Operator
NOT 运算符
As you learn more about development and logical operations, you'll learn about the NOToperator or !in C#. The usage of this operator is the most widely accepted in the industry for multiple reasons. It's simple, it's easy to read, it's easy to understand, and it's fast:
当你了解开发和逻辑运算,您将了解NOT运营商或!在C#。由于多种原因,该运算符的使用在行业中被最广泛接受。它很简单,易于阅读,易于理解,而且速度很快:
x != x
x = !x;
This breaks down to !true = falseand !false = true.
这分解为!true = false和!false = true。
The XOR Operator
异或运算符
The XORor ^in C#is the less-chosen operator for this task, but it is an acceptable operator depending on the situation. As some have pointed out in comments, this operator is especially useful when dealing with longer names or deep-access members. If performance matters, and the operation will be executed multiple times, it's best to go with the NOToperator, as it is faster. If performance isn't really a concern, then using the XORoperator is acceptable; though in a professional environment you'll likely need to justify the usage.
的XOR或^在C#为这个任务所选择的较少的操作员,但它是根据具体情况的可接受的操作者。正如一些人在评论中指出的那样,这个运算符在处理较长的名称或深度访问成员时特别有用。如果性能很重要,并且操作将执行多次,最好与NOT操作员一起使用,因为它更快。如果性能不是真正的问题,那么使用XOR运算符是可以接受的;尽管在专业环境中,您可能需要证明使用的合理性。
b ^= true;
b = b ^ true;
The biggest thing to understand about XORprior to using it for a toggle, is understanding how it works logically:
XOR在将其用于切换之前要了解的最重要的事情是了解它的逻辑工作方式:
true ^ false = true;
true ^ true = false;
false ^ true = true;
false ^ false = false;
That logic table explains why b ^= falsewon't toggle. A XORrequires one side of the operator to be trueand the other to be false. Which side it is doesn't matter, but neither side can be the same value or it will evaluate to false. Hence:
该逻辑表解释了为什么b ^= false不会切换。甲XOR需要操作者的一侧是true,另一个是false。它是哪一边并不重要,但任何一边都不能是相同的值,否则它将评估为false。因此:
bool b = true;
b ^= false; // true.
b ^= false; // true. So on, and so forth.
bool b = false;
b ^= false; // false.
b ^= false; // false. So on, and so forth.
Final Thoughts
最后的想法
The answer to the OP's question is simply:
OP问题的答案很简单:
- The
NOToperator is the cleanest and most efficient solution for toggling.
- 该
NOT操作是用于切换最清洁和最高效的解决方案。
I wish you all the best of luck in your future endeavors, and hopefully this answer helped to shed further light on the topic!
我祝你在未来的努力中一切顺利,希望这个答案有助于进一步阐明这个话题!

