为什么字符串属性上的GetType会导致NullReferenceException?

2 c# properties gettype nullreferenceexception

当我在int-或DateTime属性上调用GetType时,我得到了预期的结果,但是在字符串属性上,我得到一个NullReferenceException(?):

private int      PropInt    { get; set; }
private DateTime PropDate   { get; set; }
private string   propString { get; set; }

WriteLine(PropInt.GetType().ToString());    // Result : System.Int32
WriteLine(PropDate.GetType().ToString());   // Result : System.DateTime
WriteLine(propString.GetType().ToString()); // Result : NullReferenceException (?!)
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下怎么来?字符串支柱与int-prop有什么不同?

Ada*_*son 8

如果属性的值是null,那么在尝试访问对象方法或属性时,您将获得NullReferenceException,例如GetType().原始类型喜欢intDateTime是值类型,因此不能保存null值,这就是为什么GetType()不会失败的原因除了任何其他成员函数.