简体   繁体   中英

C#. How to assign a bool variable certain value (not true and not false)?

How to assign to a bool variable certain value (not true and not false)? I guess it can be so-called null state. But I'm not sure if it's correct guess. And I have no clue how to do that. Moreover, how to do that with possible further assignation (true/false)?

You need to declare a nullable boolean.

bool? x = null;

.NET Docs on nullable types

布尔值是一种值类型,如果要使用null进行初始化,则可以声明可为null的布尔变量,例如:

bool? x

That's right

Types that can be set to NULL are instances of the System.nullable structure.

T? variable = null;

troubleshooting

Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

has already been answered here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM