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

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

selectedItem has two fields:

  • int? _cost
  • string _serialNumber

In this example, _cost

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

    Because string type's null really points to nothing, there isn't any object in memory.
    But int? type(nullable) even with value set to null still points to some object.
    If you read Jeffrey Richter's "CLR via C#" you'll find out that nullable type are just facade classes for common types with some incapsulated logics in order to make work with DB null more convenient.

    Check msdn to learn about nullable types.

提交回复
热议问题