转换为空的布尔?为bool [英] Convert nullable bool? to bool

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

问题描述

你如何转换为空的布尔?布尔在C#?

How do you convert a nullable bool? to bool in C#?

我曾尝试 x.Value x.HasValue ...

推荐答案

您最终还是要决定什么空布尔将代表。如果,你可以这样做:

You ultimately have to decide what the null bool will represent. If null should be false, you can do this:

bool newBool = x.HasValue ? x.Value : false;

或者

bool newBool = x.HasValue && x.Value;

或者

bool newBool = x ?? false;

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

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