c# - 如何判断对象引用是否为空?

标签 c# object null visual-studio-2005 object-reference

确定对象引用变量是否为 null 的最佳方法是什么?

是下面的吗?

MyObject myObjVar = null;
if (myObjVar == null)
{
    // do stuff
}

最佳答案

是的,你是对的,如果你想执行任意代码,下面的代码片段是可行的方法:

MyObject myObjVar; 
if (myObjVar == null) 
{ 
    // do stuff 
} 

顺便说一句:您的代码不会像现在这样编译,因为 myObjVar 在初始化之前被访问。

关于c# - 如何判断对象引用是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12000228/

相关文章:

javascript - 为什么我收到此错误 : Uncaught TypeError: cannot read property 'john' of undefined

python - spark 中 null 和 NaN 之间的区别?如何处理?

java - 确保 Type 实例代表可从某个类分配的类型

c# - 使用 C# 抑制 Excel VSTO 中的 "number stored as text"警告

javascript - 未捕获的类型错误 : Object `function()` has no method 'call' ?

javascript - 仅在定义值时才内联设置 JS 对象的属性

android - 尝试在安卓手机上设置屏幕亮度

sql - 为什么 SQL 不支持 "= null"而不是 "is null"?

c# - 将 Newtonsoft.Json.Linq.JArray 转换为特定对象类型的列表

c# - 如何在 Entity Framework 中添加种子方法