C#
casting
boolean
integer
programming error

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:

csharp
bool isReady = true;
int value = (int)isReady;

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:

csharp
1bool isReady = true;
2
3int numericValue = isReady ? 1 : 0;
4
5Console.WriteLine(numericValue); // 1

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:

csharp
1bool isReady = false;
2
3int numericValue = Convert.ToInt32(isReady);
4
5Console.WriteLine(numericValue); // 0

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:

csharp
1using System;
2using System.Linq;
3
4bool[] flags = { true, false, true, true };
5
6int[] bits = flags.Select(Convert.ToInt32).ToArray();
7
8Console.WriteLine(string.Join(", ", bits)); // 1, 0, 1, 1

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:

csharp
1bool hasAccess = CheckUserAccess();
2
3if (hasAccess)
4{
5    Console.WriteLine("Open the dashboard");
6}

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:

csharp
1public static class BoolExtensions
2{
3    public static int ToInt(this bool value)
4    {
5        return value ? 1 : 0;
6    }
7}
8
9bool isEnabled = true;
10int enabledValue = isEnabled.ToInt();

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 bool to int.
  • Use value ? 1 : 0 when you want an explicit, readable mapping.
  • Use Convert.ToInt32(value) when a built-in conversion is convenient.
  • Keep values as bool unless a numeric representation is actually required.
  • Define conversion behavior clearly at system boundaries such as databases, files, and external APIs.

Course illustration
Course illustration

All Rights Reserved.