当您尝试访问成员字段或指向null的函数类型时,会发生NullReferenceException。
这是一个例子-
using System;
class Demo {
static void Main() {
string str = null;
if (str.Length > 0) {
Console.WriteLine(str);
}
}
}输出结果
以下是输出。它抛出NullReferenceException,因为您在tryonhg中访问指向null的memebt-
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in <0bc5fbf292484d5194a19866ae5c2018>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in <0bc5fbf292484d5194a19866ae5c2018>:0