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

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

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


当前回答

  public static bool isnull(object T)
  {
      return T == null ? true : false;
  }

use:

isnull(object.check.it)

有条件的使用:

isnull(object.check.it) ? DoWhenItsTrue : DoWhenItsFalse;

更新(另一种方式)更新于2017年8月31日和2021年1月25日。谢谢你的评论。

public static bool IsNull(object T)
{
    return (bool)T ? true : false;
}

骨干示范

为了记录,你在Github上有我的代码,去看看吧: https://github.com/j0rt3g4/ValidateNull PS:这一点是特别为你准备的,Chayim Friedman,不要使用测试软件,假设这一切都是真的。等待最终版本或使用您自己的环境进行测试,然后在没有任何文档或演示的情况下假设真正的beta软件。

其他回答

在。net 6中有一个一行程序

ExampleMethod(null);

void ExampleMethod(object param)
{
    ArgumentNullException.ThrowIfNull(param);
    // Do something
}

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

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

Jeffrey L Whitledge是对的。你的' dataList ' -Object本身是空的。

您的代码还有另一个问题:您使用了ref-关键字,这意味着参数数据不能为空!MSDN说:

传递给ref形参的参数必须首先初始化。这与out不同,后者的参数在传递之前不必显式初始化

在“Object”类型中使用泛型也不是一个好主意。泛型应该避免装箱/拆箱,并确保类型安全。如果你想要一个公共类型,让你的方法泛型。最后,你的代码应该是这样的:

public class Foo<T> where T : MyTypeOrInterface {

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

      public bool AddData(ref T data) {
        bool success = false;
        try {
          dataList.Add(data);                   
          success = doOtherStuff(data);
        } catch (Exception e) {
          throw new Exception(e.ToString());
        }
        return success;
      }

      private bool doOtherStuff(T data) {
        //...
      }
    }
  public static bool isnull(object T)
  {
      return T == null ? true : false;
  }

use:

isnull(object.check.it)

有条件的使用:

isnull(object.check.it) ? DoWhenItsTrue : DoWhenItsFalse;

更新(另一种方式)更新于2017年8月31日和2021年1月25日。谢谢你的评论。

public static bool IsNull(object T)
{
    return (bool)T ? true : false;
}

骨干示范

为了记录,你在Github上有我的代码,去看看吧: https://github.com/j0rt3g4/ValidateNull PS:这一点是特别为你准备的,Chayim Friedman,不要使用测试软件,假设这一切都是真的。等待最终版本或使用您自己的环境进行测试,然后在没有任何文档或演示的情况下假设真正的beta软件。

[编辑以反映@kelton52的提示]

最简单的方法是做对象。ReferenceEquals (null,数据)

由于(null==data)不保证工作:

class Nully
{
    public static bool operator ==(Nully n, object o)
    {
        Console.WriteLine("Comparing '" + n + "' with '" + o + "'");
        return true;
    }
    public static bool operator !=(Nully n, object o) { return !(n==o); }
}
void Main()
{
    var data = new Nully();
    Console.WriteLine(null == data);
    Console.WriteLine(object.ReferenceEquals(null, data));
}

生产:

比较"和" Nully " 真正的 假