简体   繁体   中英

c# assign string value based on bool value which can be nullable

Here is my code:

    stringValue = microData.BoolValue.HasValue ? "True" : "False";

What I am trying to do is assign a stringvalue based on a Boolvalue. if BoolValue has a value I like to do the following:

Assign stringValue = "True" if BoolValue is true. Assign stringValue = "False" ib BoolValue is false.

If Boolvalue does not have a value, assign it to null.

What i have above does not seem to work.

stringValue = microData.BoolValue.HasValue ?
              microData.BoolValue.ToString() :
              (string)null;

尝试这个:

stringValue = BoolValue.HasValue ? BoolValue.Value ? "True" : "False" : null;

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