如何将可为空的布尔值转换为整数? [英] How to convert nullable bool to intgere ?

查看:100
本文介绍了如何将可为空的布尔值转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将可为空的布尔值转换为intgere?

How to convert nullable bool to intgere ?

推荐答案

错误的问题,取决于您想要的内容,没有预定义的转换.

可为空的bool具有三个值:nulltruefalse.检查所有三种情况的值(例如,在开关块中),并返回想要的任何三个不同的整数,例如0,-1和9000.:-)

—SA
Incorrect question, it depends on what you want, there is no predefined conversion.

Nullable bool has three values: null, true or false. Check the value all three cases (in switch block, for example) and return any three different integer numbers you want, for example 0, -1 and 9000. :-)

—SA


即使它不是可为空的类型,也不会从布尔型转换为int型,因此您需要手动进行操作. br/>
Even if it wasn''t a nullable type, there is no conversion form a bool to an int, so you need to do it manually.
bool? b = true;
int i =  (b ?? false) ? 1 : 0;


这篇关于如何将可为空的布尔值转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆