Why does .ToString() on a null string cause a null error, when .ToString() works fine on a nullable int with null value?

前端 未结 8 1250
耶瑟儿~
耶瑟儿~ 2020-11-30 12:19

selectedItem has two fields:

  • int? _cost
  • string _serialNumber

In this example, _cost

8条回答
  •  春和景丽
    2020-11-30 12:42

    The Nullable is actually a struct exposing two properties: HasValue and Value. If you do this you will get your error:

    int? i = null;
    i.Value.ToString()
    

    In order to check whether or not your int? has a value you can access i.HasValue

提交回复
热议问题