[英]What does the ! mean in this context?
button.Click += delegate {
blue_panel.Visible = !blue_panel.Visible;
};
What does the '!' 什么是'!' in bluepanel.Visible indicate? 在bluepanel.Visible指示? What does this statement mean and what are alternative ways of writing it? 这句话意味着什么,以及它的替代方式是什么?
The ! 的! symbol is the NOT (or logical negation) operator in C#, see ! symbol是C#中的NOT(或逻辑否定)运算符,请参阅! Operator for more information. 运营商了解更多信息。
In this particular case, it means set the visibility of the blue_panel object to the inverse of what it was previously (true if false, false if true). 在这种特殊情况下,它意味着将blue_panel对象的可见性设置为之前的反转(如果为false则为true,如果为true则为false)。 Essentially its a quick way of toggling a Boolean between its two possible values, commonly used to show/hide things. 基本上它是一种在两个可能值之间切换布尔值的快速方法,通常用于显示/隐藏事物。
There's a few different ways to write it, but why would you want to change it? 有几种不同的方式来编写它,但为什么要改变呢? Its a very concise way of representing a Boolean toggle. 它是一种表示布尔切换的非常简洁的方式。
! ! is a logical negation operator . 是一个逻辑否定运算符 。
The logical negation operator (!) is a unary operator that negates its operand. 逻辑否定运算符(!)是一个否定其操作数的一元运算符。 It is defined for bool and returns true if and only if its operand is false. 它是为bool定义的,当且仅当其操作数为false时才返回true。
In your sample, it is toggling the current state of your panel. 在您的示例中,它切换了面板的当前状态。 If it is currently visible than it will be hidden and vice versa. 如果它当前可见,则它将被隐藏,反之亦然。
!
is a NOT
operator , it makes inverse
operasion. 它是一个NOT
运算符,它使inverse
操作。
So it basically Toggles the state of the blue_panel
Visibility. 所以它基本上切换了blue_panel
可见性的状态。
Here : 这里 :
blue_panel.Visible = !blue_panel.Visible;
assigns false
if blue_panel
is visible
, else assign true
. 如果blue_panel
visible
则指定false
,否则指定true
。
it can be rewritten as : 它可以改写为:
Method 1: 方法1:
if(blue_panel.Visible)
blue_panel.Visible = false;
else
blue_panel.Visible = true;
Method 2: 方法2:
blue_panel.Visible = blue_panel.Visible?false:true;
! ! usually means not, but in this instance it is used to flip a boolean. 通常意味着没有,但在这种情况下,它用于翻转布尔值。
it says if Visible is true, make it false. 它说如果Visible是真的,那就把它弄错。 If its false make it true. 如果它的错误使其成为现实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.