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

在下面的代码中,我检查对象是否为空:

if (!data.Equals(null))

and

if (data != null)

然而,我在datlist . add (data)收到一个NullReferenceException。如果对象为空,它甚至不应该输入If语句!

因此,我在问这是否是检查对象是否为空的正确方法:

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

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


当前回答

你的dataList是空的,因为它还没有被实例化,从你发布的代码判断。

Try:

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

其他回答

正如其他人已经指出的那样,它不是data,而是dataList是null。除此之外……

catch-throw is an antipattern that almost always makes me want to throw up every time that I see it. Imagine that something goes wrong deep in something that doOtherStuff() calls. All you get back is an Exception object, thrown at the throw in AddData(). No stack trace, no call information, no state, nothing at all to indicate the real source of the problem, unless you go in and switch your debugger to break on exception thrown rather than exception unhandled. If you are catching an exception and just re-throwing it in any way, particularly if the code in the try block is in any way nontrivial, do yourself (and your colleagues, present and future) a favor and throw out the entire try-catch block. Granted, throw; is better than the alternatives, but you are still giving yourself (or whoever else is trying to fix a bug in the code) completely unnecessary headaches. This is not to say that try-catch-throw is necessarily evil per se, as long as you do something relevant with the exception object that was thrown inside the catch block.

然后,首先就存在捕获Exception的潜在问题,但这是另一回事,特别是因为在这种特殊情况下,您抛出了一个异常。

另一件让我感到非常危险的事情是,数据可能在函数执行期间更改值,因为您是通过引用传递的。因此,null检查可能会通过,但在代码对值进行任何操作之前,它已经被更改了——可能是null。我不确定这是否值得关注(可能不是),但这似乎值得关注。

从c# 8开始,你可以使用'empty'属性模式(带模式匹配)来确保对象不为空:

if (obj is { })
{
    // 'obj' is not null here
}

这种方法意味着“如果对象引用了某个对象的实例”(即它不是空的)。

你可以把它看作是:if (obj is null)....的对立面当对象没有引用某个对象的实例时,返回true。

有关c# 8.0模式的更多信息,请阅读这里。

我只是遵循了我们通常在java脚本中遵循的方法。将对象转换为字符串,然后检查它们是否为空。

var obj = new Object();
var objStr = obj.ToString();
if (!string.IsNullOrEmpty(objStr)){
  // code as per your needs
}

以下是我使用的一些扩展:

/// <summary>
/// Extensions to the object class
/// </summary>
public static class ObjectExtensions
{
    /// <summary>
    /// True if the object is null, else false
    /// </summary>
    public static bool IsNull(this object input) => input is null;

    /// <summary>
    /// False if the object is null, else true
    /// </summary>
    public static bool NotNull(this object input) => !IsNull(input);
}

从c# 9开始你就可以做到

if (obj is null) { ... }

非空用

if (obj is not null) { ... }

如果需要重写此行为,请相应使用==和!=。