Is there any difference between type? and Nullable?

前端 未结 9 1136
礼貌的吻别
礼貌的吻别 2020-12-15 15:53

In C# are the nullable primitive types (i.e. bool?) just aliases for their corresponding Nullable type or is there a difference between th

9条回答
  •  醉梦人生
    2020-12-15 16:13

    To access the value of the bool? you need to do the following:

    bool? myValue = true;
    bool hasValue = false;
    
    if (myValue.HasValue && myValue.Value)
    {
      hasValue = true;
    }
    

    Note you can't just do:

    if (myValue)
    {
      hasValue = true;
    }
    

提交回复
热议问题