在 C# 中检查对象是否为空

Checking if an object is null in C#(在 C# 中检查对象是否为空)
本文介绍了在 C# 中检查对象是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

if (!data.Equals(null))

if (data != null)

但是,我在 dataList.Add(data) 收到一个 NullReferenceException.如果对象为空,它甚至不应该进入 if 语句!

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

public List数据列表;public bool AddData(ref 对象数据)布尔成功=假;尝试{//我也用过 "if (data != null)" 也没有用如果 (!data.Equals(null)){//此处发生NullReferenceException ...dataList.Add(data);成功 = doOtherStuff(data);}}捕获(例外 e){抛出新的异常(e.ToString());}返回成功;}

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

解决方案

null 不是data,而是dataList.

您需要创建一个

public ListdataList = new List();

更好:因为它是一个字段,所以将其设为私有.如果没有什么可以阻止您,请将其设置为 readonly.只是很好的做法.

旁边

检查无效性的正确方法是if(data != null).这种检查在引用类型中无处不在;甚至 Nullable<T> 覆盖了相等运算符,以便在检查 null 性时更方便地表达 nullable.HasValue.

如果你执行 if(!data.Equals(null)) 那么你会得到一个 NullReferenceException 如果 data == null.这有点可笑,因为避免这种例外是首要目标.

你也在这样做:

catch(异常 e){抛出新的异常(e.ToString());}

这绝对不好.我可以想象你把它放在那里只是为了你可以在方法内部时进入调试器,在这种情况下忽略这一段.否则,不要无缘无故地捕获异常.如果你这样做了,只需使用 throw; 重新抛出它们.

I would like to prevent further processing on an object if it is 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). If the object was null, it should never have even entered the if-statement!

Thus, I'm asking if this is proper way of checking if an object is 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)?

解决方案

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

You need to create one with

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

Even better: since it's a field, make it private. And if there's nothing preventing you, make it also readonly. Just good practice.

Aside

The correct way to check for nullity is 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.

If you do if(!data.Equals(null)) then you will get a NullReferenceException if data == null. 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;.

这篇关于在 C# 中检查对象是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)