Question about bool

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
this works very well,

is it true to say if a bool is false, but then gets a value that it becomes true?

Somebody answered me something similar in another thread, but i just wanted to make sure.

C#:
            bool hasName = false;
            

            while (!hasName)
            {
                managerName = ConsoleManager.GetLimitedString("Please enter your name (5 to 15 characters):", 5, 15);
                hasName = ConsoleManager.GetYesNo("Are you happy with " + managerName + " as your managers name?");
            }
 
is it true to say if a bool is false, but then gets a value that it becomes true?

No. Getting the value of a boolean (or any other C# primitive type for that matter) does not change its value. (You could create a custom C# value type that does this crazy behavior of changing values each time you read it, but I think people will come to your door with torches and pitchforks because it breaks the principle of least surprise that the C# community tries adhere to when designing things.)

!hasName is the same as hasName == false because it's the same as hasName != true. It's just the idiomatic way C and C++ programmers like to write checks to see if a boolean is set to false, and it's carried over to C#. (From my understanding, Java programmers insist that you write it as hasName == false even though it also supports the idiomatic way of writing the check.)
 
I'm not sure that @Skydiver has interpreted the question correctly, or maybe I haven't. It sounds to me as though you're saying that a bool does not have a value if it false and does have a value if it is true. That is one interpretation but I'm not sure that it's a good one. If I'm not mistaken, a bool variable is stored as a 32-bit number. Either all 32-bits are 0 and the variable is false or all 32-bits are set to 1 and the variable is true. To say that a bool that's false has no value is like saying that an int that's zero has no value. It's true in one sense but not in another. You may hear some people talk about "setting" and "resetting" a bool to mean setting it to true and false respectively, so that does sort of support the idea that only true is a value, but it is also consistent with considering false to be the default value (being filled with all zeroes is the default for any variable but is interpreted differently based on type) so setting any bits to 1 in that variable sets its value to something and resetting every bit to 0 resets its value to the default.
 
Just a minor quibble: I believe that it was only VB6 which required true for all bits in its native representation to be on to signify true. The CLI's System.Boolean, which C# uses as its bool, uses only a byte (8-bits) and only requires at least one of the bits within a byte to be considered true. See SO answer.
 
Just a minor quibble: I believe that it was only VB6 which required true for all bits in its native representation to be on to signify true. The CLI's System.Boolean, which C# uses as its bool, uses only a byte (8-bits) and only requires at least one of the bits within a byte to be considered true. See SO answer.
That probably follows from C/C++, where anything could be treated as a Boolean and was considered false if it was all zeroes and true otherwise. C# won't allow that at a high-level (as far as I'm aware) but I guess it still happens that way under the hood.
 
Sorry guys its me again not looking further than my nose

its calling the yes no method which is returning a value and breaking out of the loop.

but all that was said here was of course very useful, thanks for the help.
 
Back
Top Bottom