Concept of true,false logically and numerically?
I was asked this question in an interview! I just wanted to know what the right answer to this is. I told that logically the concept is represented by bool data type(C#).A variable of bool data type can have true or false value and can be used as a conditional check condition.Numerica开发者_运维问答lly, 1 represents true and 0 represents false in most programming languages.I don't know what else to add or what is the distinction between the two.Any comments will be greatly appreciated.
In C# (unlike some other languages) booleans are not integers and are not convertible to integers:
int x = true; // Error - Cannot implicitly convert type 'bool' to 'int'
As a result it doesn't make sense to say that true is equal to 1 in C#. At an implementation level the value true might be stored internally as the value 1, but this is a detail specific to that implementation, not a feature of C# itself.
If you want to convert a boolean to the value 0 or 1 you can do this:
int x = isFoo ? 1 : 0;
精彩评论