Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing bool values to opposite of the initial value

Tags:

c++

c

c#

boolean

This maybe sound strange to you but I'm too lazy to write everytime like

if (threadAlive) {             threadAlive = false; }         else {             threadAlive = true; } 

isn't there is something like int++ or int-- to change bool value to opposite of its value?

like image 331
user1046403 Avatar asked Aug 02 '13 13:08

user1046403


People also ask

How do you invert a Boolean value?

Invert a boolean method or propertyRight-click and choose Refactor | Invert Boolean from the context menu. Choose ReSharper | Refactor | Invert Boolean… from the main menu.

How do you make a Boolean opposite?

The operator. not_() function takes a boolean value as its argument and returns the opposite of that value.

Is bool initialized to false?

To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.

Can you set a bool to null?

C# has two different categories of types: value types and reference types. Amongst other, more important distinctions, value types, such as bool or int, cannot contain null values.


1 Answers

Just do this:

threadAlive = !threadAlive; 
like image 67
p.s.w.g Avatar answered Oct 01 '22 07:10

p.s.w.g