c# - 为什么这两行程序会出现 NullPointerException?

标签 c#

class Program
{
    static void Main(string[] args)
    {
        Func<Object> someMethod = new Func<Object>(((Object)null).ToString);
        String nameOfMethod = someMethod.Method.Name;
    }
}

我不确定为什么 someMethod 函数的主体会执行。

最佳答案

Func<Object> someMethod = new Func<Object>(((Object)null).ToString);

它没有执行,但您尝试访问空引用上的方法 ToString()

关于c# - 为什么这两行程序会出现 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5438443/

相关文章:

c# - 使用 ffmpeg 时如何提高图像导出/提取速度?

c# - Entity Framework 将导航属性设置为 null

c# - Control.Invoke 在第二次调用时失败

c# - list.First()、list.ElementAt(0) 和 list[0] 的区别?

c# - MVVM - 按钮命令绑定(bind)不起作用

c# - 在 Dapper Multiple Results 中,当 where 子句取决于第一个查询的结果时,如何让第二行查询工作?

c# - C# 中跨平台类的设计

c# - 在 MVC4 View 中显示图像

c# - Linq 从 View 中选择行?

c# - 使用 LDAP 查询从我公司的所有林中获取所有域