Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c# - 在C#中检查对象是否为空(Checking if an object is null in C#)

I would like to prevent further processing on an object if it is null.

(如果对象为null,我想防止对其进行进一步处理。)

In the following code I check if the object is null by either:

(在以下代码中,我通过以下任一方法检查对象是否为空:)

if (!data.Equals(null))

and

(和)

if (data != null)

However, I receive a NullReferenceException at dataList.Add(data) .

(但是,我在dataList.Add(data)处收到NullReferenceException 。)

If the object was null, it should never have even entered the if -statement!

(如果该对象为null,则它甚至不应输入if -statement!)

Thus, I'm asking if this is proper way of checking if an object is null:

(因此,我问这是否是检查对象是否为null的正确方法:)

public List<Object> dataList;
public  bool AddData(ref Object data)
    bool success = false;
    try
    {
        // I've also used "if (data != null)" which hasn't worked either
        if (!data.Equals(null))
        {
           //NullReferenceException occurs here ...
           dataList.Add(data);
           success = doOtherStuff(data);
        }
    }
    catch (Exception e)
    {
        throw new Exception(e.ToString());
    }
    return success;
}

If this is the proper way of checking if the object is null, what am I doing wrong (how can I prevent further processing on the object to avoid the NullReferenceException)?

(如果这是检查对象是否为null的正确方法,那我在做错什么(如何防止对该对象进行进一步处理以避免NullReferenceException)?)

  ask by developer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not data that is null , but dataList .

(不是datanull ,而是dataList 。)

You need to create one with

(您需要创建一个)

public List<Object> dataList = new List<Object>();

Even better: since it's a field, make it private .

(甚至更好:因为这是一个领域,所以将其private 。)

And if there's nothing preventing you, make it also readonly .

(如果没有什么阻止您,请将其设置为readonly 。)

Just good practice.

(只是好的做法。)

Aside

(在旁边)

The correct way to check for nullity is if(data != null) .

(检查无效性的正确方法是if(data != null) 。)

This kind of check is ubiquitous for reference types;

(对于引用类型,这种检查无处不在。)

even Nullable<T> overrides the equality operator to be a more convenient way of expressing nullable.HasValue when checking for nullity.

(即使Nullable<T>覆盖相等运算符,也是检查nullable.HasValue时表达nullable.HasValue的更便捷方法。)

If you do if(!data.Equals(null)) then you will get a NullReferenceException if data == null .

(如果执行if(!data.Equals(null))则如果data == null则将收到NullReferenceException 。)

Which is kind of comical since avoiding this exception was the goal in the first place.

(之所以有点可笑,是因为首先避免了此异常。)

You are also doing this:

(您也正在这样做:)

catch (Exception e)
{
    throw new Exception(e.ToString());
}

This is definitely not good.

(这绝对不好。)

I can imagine that you put it there just so you can break into the debugger while still inside the method, in which case ignore this paragraph.

(我可以想象您将其放在此处,以便可以在仍位于方法内部时进入调试器,在这种情况下,请忽略本段。)

Otherwise, don't catch exceptions for nothing.

(否则,不要一无所获。)

And if you do, rethrow them using just throw;

(如果您这样做,则只需throw;它们throw;)

.

(。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...