Check if null Boolean is true results in exception

后端 未结 7 1398
难免孤独
难免孤独 2020-11-28 05:20

I have the following code:

Boolean bool = null;

try 
{
    if (bool)
    {
        //DoSomething
    }                   
} 
catch (Exception e) 
{
    Syst         


        
7条回答
  •  余生分开走
    2020-11-28 05:52

    Boolean types can be null. You need to do a null check as you have set it to null.

    if (bool != null && bool)
    {
      //DoSomething
    }                   
    

提交回复
热议问题