Reputation: 17
button.Click += delegate {
blue_panel.Visible = !blue_panel.Visible;
};
What does the '!' in bluepanel.Visible indicate? What does this statement mean and what are alternative ways of writing it?
Upvotes: 0
Views: 127
Reputation: 32729
! 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.
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.
Upvotes: 1
Reputation: 1574
The ! symbol is the NOT (or logical negation) operator in C#, see ! 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). 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.
Upvotes: 1
Reputation: 1093
! usually means not, but in this instance it is used to flip a boolean.
it says if Visible is true, make it false. If its false make it true.
Upvotes: 0
Reputation: 26209
!
is a NOT
operator , it makes inverse
operasion.
So it basically Toggles the state of the blue_panel
Visibility.
Here :
blue_panel.Visible = !blue_panel.Visible;
assigns false
if blue_panel
is visible
, else assign true
.
it can be rewritten as :
Method 1:
if(blue_panel.Visible)
blue_panel.Visible = false;
else
blue_panel.Visible = true;
Method 2:
blue_panel.Visible = blue_panel.Visible?false:true;
Upvotes: 0