Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Convert.ToInt32(Boolean) — convert bool to 0 or 1

Convert.ToInt32(Boolean) converts false to 0 and true to 1. This static overload on System.Convert returns int and lets you explicitly represent a Boolean value as a number.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

public static int ToInt32(bool value);

Convert.ToInt32(Boolean) is an overload of a static method on System.Convert. In its C# signature, the parameter type is bool and the return type is int: public static int ToInt32(bool value);. The method returns 0 for false and 1 for true.

Use it when a data consumer requires a numeric flag rather than a Boolean value. The result is a signed 32-bit integer, not text. For both possible bool values, the result fits in int, so this conversion does not overflow.

The argument type matters: this entry concerns Boolean, not the overloads accepting text or other numbers. Pass a bool value, as in the example. The example prints 0 first and then 1, each on its own line.

Examples

using System;

internal static class Program
{
    private static void Main()
    {
        bool disabled = false;
        bool enabled = true;

        int disabledValue = Convert.ToInt32(disabled);
        int enabledValue = Convert.ToInt32(enabled);

        Console.WriteLine(disabledValue);
        Console.WriteLine(enabledValue);
    }
}

Result

0
1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources