I know this has probably already been answered somewhere, I just can’t find it. Why doesn’t C# allow me to use while(1)? I know that ‘There is no conversion between the bool type and other types’ in C# but why? What are the reasons for this, when in c++ it’s perfectly acceptable.
I know this has probably already been answered somewhere, I just can’t find it.
Share
It is to keep programmers from accidentally doing something they did not want to do.
Consider this common pitfall in C/C++:
This will compile and run, but produce unexpected results (the programmer likely meant to check that
xequals10, not assign tox— otherwise, why would he need the test at all? It will always evaluate totrue).By forcing conditionals to be of boolean type, you avoid this issue.