+ 2

Difference between bool check=true; and bool check=1;

31st Dec 2016, 12:39 PM
Anurag Regmi
Anurag Regmi - avatar
1 Odpowiedź
+ 3
As quoted from MSDN (https://msdn.microsoft.com/en-us/library/c8f5xwh7.aspx): "In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. In C#, there is no conversion between the bool type and other types. For example, the following if statement is invalid in C#" In C/C++, the value of bool and int are convertible but not in C#. So the following statements: C++: bool check = true; // or 1 if (check) cout<<"This is acceptable for both true or 1 in c++"; but, C#: int x = 123; // if (x) // Error: "Cannot implicitly convert type 'int' to 'bool'" { Console.Write("The value of x is nonzero."); } Hope this helps.
31st Dec 2016, 12:46 PM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar