多级Null检查 [英] Multiple levels of Null Checking
问题描述
我发现自己总是这样做:
if(
SomeObject!= null&&
SomeObject.AnotherObject!= null&&
SomeObject.AnotherObject.YetAnother!= null&&
SomeObject.AnotherObject.YetAnother.HelpMePlease!= null )
{
//做点什么
}
当然必须有更好的方法我失踪了?我想到了
使用声明,但那当然取决于IDisposable接口
正在实现,对吧?
谢谢!
I find myself doing things like this all the time:
if (
SomeObject != null &&
SomeObject.AnotherObject != null &&
SomeObject.AnotherObject.YetAnother != null &&
SomeObject.AnotherObject.YetAnother.HelpMePlease != null)
{
// Do Something
}
Surely there must be a better way that I am missing? I thought about the
using statement, but that of course depends upon the IDisposable interface
being implemented, right?
Thanks!
推荐答案
Karch< no **** @ absotutely.comwrote:
Karch <no****@absotutely.comwrote:
我发现自己总是这样做:
if(
SomeObject!= null&&
SomeObject.AnotherObject!= null&&
SomeObject.AnotherObject.YetAnother!= null&&
SomeObject.AnotherObject.YetAnother.HelpMePlease!= null)< br $>
{
//做点什么
}
肯定有一个更好的方法我我不见了?我想到了
使用语句,但这当然取决于IDisposable接口
正在实施,对吗?
I find myself doing things like this all the time:
if (
SomeObject != null &&
SomeObject.AnotherObject != null &&
SomeObject.AnotherObject.YetAnother != null &&
SomeObject.AnotherObject.YetAnother.HelpMePlease != null)
{
// Do Something
}
Surely there must be a better way that I am missing? I thought about the
using statement, but that of course depends upon the IDisposable interface
being implemented, right?
这表明每个属性都可以合法地为空 -
总是如此?我经常发现我的大多数属性
应该总是非null,在这种情况下,如果我尝试取消引用,我很高兴抛出一个
NullReferenceException它和它的价格是
null。
像上面这样的代码偶尔会发生,但不会*经常发生。至于另外一点,我很少会使用4级深度的属性 - 我通常会尝试设计类,以便我可以让一个班级做什么
我很感兴趣,没有经过多个级别。
-
Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet博客: http://www.msmvps.com/jon.skeet
如果回复小组,请不要给我发邮件
That suggests that each of those properties can legitimately be null -
is that always the case? I usually find that most of my properties
should always be non-null, in which case I''m happy for a
NullReferenceException to be thrown if I try to dereference it and it''s
null.
Code like the above happens occasionally, but not *that* often. As
another point, I rarely end up using properties 4 levels deep - I
usually try to design classes so that I can ask one class to do what
I''m interested in, without going through several levels.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
On Tue,2006年11月14日13:19:07 -0600, " Karch" < no **** @ absotutely.com>
写道:
On Tue, 14 Nov 2006 13:19:07 -0600, "Karch" <no****@absotutely.com>
wrote:
>我发现自己一直这样做:
if(
SomeObject!= null&&
SomeObject.AnotherObject!= null&&
SomeObject.AnotherObject.YetAnother!= null& ;&
SomeObject.AnotherObject.YetAnother.HelpMePleas e!= null)
$
//做点什么
}
当然我必须有一个更好的方式让我失踪?我想到了
使用声明,但这当然取决于IDisposable接口正在实施,对吧?
谢谢!
>I find myself doing things like this all the time:
if (
SomeObject != null &&
SomeObject.AnotherObject != null &&
SomeObject.AnotherObject.YetAnother != null &&
SomeObject.AnotherObject.YetAnother.HelpMePleas e != null)
{
// Do Something
}
Surely there must be a better way that I am missing? I thought about the
using statement, but that of course depends upon the IDisposable interface
being implemented, right?
Thanks!
不确定我是否理解你..如果AnotherObject是某个属性,那么某些对象是SomeObject,而SomeObject是null,我相信AnotherObject应该是
也是null
Not sure if I understand you .. if AnotherObject is a property of
SomeObject, and SomeObject is null, I believe AnotherObject should
also be null
Rad写道:
Rad wrote:
On Tue,2006年11月14日13:19:07 -0600,Karch ; < no **** @ absotutely.com>
写道:
On Tue, 14 Nov 2006 13:19:07 -0600, "Karch" <no****@absotutely.com>
wrote:
>我发现自己一直这样做:
if(
SomeObject!= null&&
SomeObject.AnotherObject!= null&&
SomeObject.AnotherObject.YetAnother!= null& ;&
SomeObject.AnotherObject.YetAnother.HelpMePleas e!= null)
{
//做点什么
}
当然必须有一个我错过了更好的方式?我想到了
使用声明,但这当然取决于正在实施的IDisposable
接口,对吧?
谢谢!
>I find myself doing things like this all the time:
if (
SomeObject != null &&
SomeObject.AnotherObject != null &&
SomeObject.AnotherObject.YetAnother != null &&
SomeObject.AnotherObject.YetAnother.HelpMePleas e != null)
{
// Do Something
}
Surely there must be a better way that I am missing? I thought about the
using statement, but that of course depends upon the IDisposable
interface being implemented, right?
Thanks!
>
不确定我是否理解你..如果AnotherObject是
SomeObject的属性,SomeObject为null,我相信AnotherObject应该
也是null
Not sure if I understand you .. if AnotherObject is a property of
SomeObject, and SomeObject is null, I believe AnotherObject should
also be null
是的,但SomeObject可能不是null,SomeObject.AnotherObject为null。
这是他正在检查的顺序。
-
Tom Porterfield
Yes, but SomeObject could be not null yet SomeObject.AnotherObject is null.
That is the order that he is checking.
--
Tom Porterfield
这篇关于多级Null检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!