C can't cast bool to int
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, bool and int are separate types with different meanings. Even though true often maps to 1 and false to 0 in lower-level representations, the language does not allow a direct cast from bool to int.
Why the Direct Cast Fails
Developers coming from C or C++ often expect this to work:
In C#, that produces a compile-time error because there is no built-in numeric conversion from bool. The type system treats a boolean as a logical value, not as a small integer. This is intentional. It keeps conditions readable and prevents code from silently mixing arithmetic and logic.
That design decision becomes useful in large codebases. A method that returns bool tells you it answers a yes or no question. A method that returns int suggests counting, indexing, or arithmetic. C# keeps those meanings distinct unless you opt into a conversion yourself.
Correct Ways to Convert bool to an Integer
If your code genuinely needs 0 or 1, use an explicit mapping. The clearest option is the conditional operator:
This is readable, fast, and leaves no doubt about the chosen representation.
Another option is Convert.ToInt32, which already knows how to convert a boolean:
Both approaches are valid. The conditional operator is more explicit when you are documenting intent in application code. Convert.ToInt32 is concise when you are transforming a collection or writing utility logic.
Converting Collections of Boolean Values
A common real-world case is converting many flags into numeric values for export, serialization, or a report. LINQ works well here:
This stays type-safe and avoids the temptation to rely on unsupported casting rules.
When You Should Not Convert at All
Sometimes the real fix is to keep the value as bool. If a variable is only used in an if statement, a numeric conversion adds noise:
Turning hasAccess into 1 or 0 here makes the code harder to read. Numeric conversion is most appropriate at boundaries, such as database imports, CSV exports, interop with another system, or legacy APIs that expect integers.
Creating a Reusable Helper
If the conversion appears frequently in one codebase, a small helper can standardize it:
This is not required, but it can improve consistency when many teams touch the same code.
Common Pitfalls
The biggest pitfall is assuming C# follows the same rules as C or JavaScript. It does not. A direct cast like (int)flag is invalid and always will be.
Another mistake is converting too early. If the value still represents a logical state, keep it as bool until the point where another API truly requires an integer. That reduces accidental comparisons such as value == 1 when a simple if (value) would have been clearer.
Be careful when storing boolean-like values in databases or JSON payloads. If one service uses true and false while another expects 1 and 0, define the mapping explicitly so the contract is obvious.
Finally, do not confuse parsing with casting. If you have the string "true", use bool.Parse or bool.TryParse first, then map the resulting boolean to an integer if needed.
Summary
- C# does not support a direct cast from
booltoint. - Use
value ? 1 : 0when you want an explicit, readable mapping. - Use
Convert.ToInt32(value)when a built-in conversion is convenient. - Keep values as
boolunless a numeric representation is actually required. - Define conversion behavior clearly at system boundaries such as databases, files, and external APIs.

