检查空对象 [英] Check for null object

查看:67
本文介绍了检查空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


检查NULL对象的最佳/正确方法是什么?





myClass测试;


if(test == null){}


这不起作用。


谢谢

Taz

Hi guys,

what is the best/correct way to check for a NULL object?

I.e.

myClass test;

if(test == null) {}

This doesnt work.

Thanks
Taz

推荐答案

你的意思是什么都行不通?这确实是确定
检查null的正确方法。


但是,我怀疑你收到编译错误告诉你

测试变量未初始化。你可能想先解决这个问题。


希望这会有所帮助。


-

- Nicholas Paldino [ .NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Tarun Mistry" < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP06.phx.gbl ...
What do you mean it doesn''t work? That is indeed the correct way to
check for null.

However, I suspect you are getting a compiler error telling you that the
test variable is not initialized. You might want to fix that first.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tarun Mistry" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...

大家好,


检查NULL对象的最佳/正确方法是什么?





myClass测试;


if(test == null){}


这不起作用。


谢谢

Taz
Hi guys,

what is the best/correct way to check for a NULL object?

I.e.

myClass test;

if(test == null) {}

This doesnt work.

Thanks
Taz



myClass test = new test();

test = null;


if(test == null)

{

System.Windows.Forms.MessageBox.Show(我是白痴);

}


Tarun Mistry < no **** @ nospam.comwrote in message

news:%2 **************** @ TK2MSFTNGP06.phx.gbl ...
myClass test = new test();
test = null;

if(test == null)
{
System.Windows.Forms.MessageBox.Show("I am an idiot");
}

"Tarun Mistry" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...

大家好,


检查NULL对象的最佳/正确方法是什么?





myClass测试;


if(test == null){}


这不起作用。


谢谢

Taz
Hi guys,

what is the best/correct way to check for a NULL object?

I.e.

myClass test;

if(test == null) {}

This doesnt work.

Thanks
Taz



Tarun Mistry< no **** @ nospam.comwrote:
Tarun Mistry <no****@nospam.comwrote:

检查NULL对象的最佳/正确方法是什么?
what is the best/correct way to check for a NULL object?



轻微的术语澄清 - 没有空的

对象。一个*引用*可以为null,但是一个对象不能。

Slight terminology clarification - there''s no such thing as a null
object. A *reference* can be null, but an object can''t.





myClass test;


if(test == null){}


这不起作用。
I.e.

myClass test;

if(test == null) {}

This doesnt work.



这正是你应该检查无效的方法。你能发一个

简短而完整的程序来证明这个问题吗?


http://www.pobox.com/~skeet/csharp/complete.html 了解详情

我的意思是什么。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该组,请不要给我发邮件

That''s precisely how you should check for nullity. Could you post a
short but complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
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


这篇关于检查空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆