Compiler Error for Nullable Bool

前端 未结 5 1237
一向
一向 2021-01-19 07:45
 bool? ispurchased = null;
    var pospurcahsed= ispurchased ? \"1\":\"2\";

Its generating exception .

Cannot implicitly con

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-19 08:32

    You can use something like this

     bool? chk = false; 
    
     //or
    
     bool? chk2 = false;
    
     var test = (chk != null && (bool)chk) ? "1": "2";
    
     //or
    
     var test2 = (chk2.HasValue && (bool)chk2) ? "1" : "2";
    

    I would suggest don't use var keyword for known types.

    Instead of this use Like this:

    String test = (chk != null && (bool)chk) ? "1" : "2";
    

提交回复
热议问题