Answers for "c# convert int in bool"

C#
1

c# int to bool

// Simple way, may crash if intValue > 1
int intValue = 1;
bool boolValue = intValue != 0;
// value of boolValue: true

// Better way
int intValue = 1;
bool boolValue = System.Convert.ToBoolean(intValue);
// value of boolValue: true
Posted by: Guest on March-18-2021
1

c# bool to int

bool t = true;
bool f = false;

var a = Unsafe.As<bool, int>(ref t);
var b = Unsafe.As<bool, int>(ref f);

Console.WriteLine(a); // 1
Console.WriteLine(b); // 0
Posted by: Guest on February-05-2022

C# Answers by Framework

Browse Popular Code Answers by Language