Share via

C# bool anomaly?

Art Hansen 621 Reputation points
2026-03-22T17:51:37.9266667+00:00

Winforms .Net Framework4.8 in VS 2022 Community 17.14.28

I’m querying  SQLite v3.46.1 with the ExecuteScalar<T> command  using the NuGet System.Data.SQLite and Dapper packages. This line of code runs fine:

            bool testVar = DBconnect.KVpair_Get_Value<bool>( queryKey );

in the data base the value for queryKey is the text false. If I display testVar via Console.WriteLine the value is False. With a breakpoint set on  the above line of code the testVar content is the C#  bool false and a conditional if (testVar) also runs fine. 

Since SQLite has “flexible typing” and  has no boolean type I expected a bool.TryParse would be needed but that doesn’t appear to be the case. Why is this working? Is it safe to continue without the bool.TryParse?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
  1. Q&A Assist
    2026-03-22T17:51:51.4133333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    bool in C# is a normal .NET System.Boolean. SQLite itself only has INTEGER/REAL/TEXT/BLOB, but the ADO.NET provider (Microsoft.Data.Sqlite or System.Data.SQLite) and Dapper sit on top and perform type mapping and conversions for you.

    From the .NET side:

    • bool is a primitive .NET type whose default is false and which is commonly used as a flag and in conditionals like if (testVar).
    • When working with databases that don’t have a native Boolean type, .NET data providers map between database primitives and .NET bool transparently.

    For SQLite specifically:

    • SQLite only stores INTEGER, REAL, TEXT, and BLOB.
    • The Microsoft SQLite provider maps a .NET Boolean to SQLite INTEGER with values 0 or 1.
    • Providers can also convert from TEXT values like "true"/"false" to bool when reading, so the caller sees a bool even though the underlying storage is TEXT.

    Dapper’s ExecuteScalar<T> relies on the underlying ADO.NET provider’s type conversion. When T is bool, it asks the provider to give a Boolean. The provider inspects the SQLite value (TEXT in this case) and converts it to false, so:

    bool testVar = DBconnect.KVpair_Get_Value<bool>(queryKey);
    

    works without needing bool.TryParse in user code.

    This is expected behavior and is safe to rely on as long as:

    • The column values are consistently valid Boolean representations for the provider (e.g., 0/1, true/false, or whatever the provider supports).
    • The schema and data format are under control so unexpected strings don’t appear.

    If there is a risk of arbitrary or malformed text in that column, reading as string and using bool.TryParse (or custom parsing) would give more explicit control and error handling. Otherwise, letting Dapper and the provider handle the conversion is standard and safe.


    References:

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 1,985 Reputation points Microsoft External Staff Moderator
    2026-03-23T07:49:03.2866667+00:00

    Hi @Art Hansen ,

    Thanks for your question.

    Your code works because System.Data.SQLite automatically converts the text value "false" (stored in SQLite) into a real C# bool false when Dapper asks for a boolean.

    Dapper passes the request to the provider, the provider recognizes "false" and does the conversion behind the scenes. This is standard, reliable behavior for this exact stack.

    Is it safe to continue without the booth.TryParse?

    • Yes, if your database always stores strictly "true"/"false" (or strictly 0/1) and never null/other strings.
    • It can become fragile if the stored values vary ("0"/"1" as TEXT, NULL, extra spaces, "yes/no"). In those cases you should normalize the data format, or read as string/int and convert explicitly.

    While this is a non-Microsoft link, it’s official SQlite documentation and is safe to visit.

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.