如何检查对象是C#.net 3.5 null或空? [英] How to check object is null or empty in C#.NET 3.5?

查看:131
本文介绍了如何检查对象是C#.net 3.5 null或空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果对象包含null或空,如何验证或检查条件是否相同?

If objects contains null or empty then how to validate or check the condition for the same?

如何为bool检查是否对象的 OBJ 空缺

How to bool check whether object obj is null or Empty

我有code如下:

class Program
{
    static void Main(string[] args)
    {
        object obj = null;

        double d = Convert.ToDouble(string.IsNullOrEmpty(obj.ToString()) ? 0.0 : obj);
        Console.WriteLine(d.ToString());
    }
}

通过这个code我得到 NullReference异常对象引用未设置到对象的实例。

With this code i'm getting NullReference Exception as Object reference not set to an instance of an object.

请帮助。

在这里,我没有变......

Here I'm not getting....

如何验证对象是否是空缺无需转换成的ToString()? ?

How to validate whether that object is null or Empty without converting into .ToString() ??

有没有检查相同??的做法

Is there an approach to check the same??

推荐答案

您所运行的问题是,你的对象类型,以及,物体。为了与string.IsNullOrEmpty来评价它,你应该通过你的对象与投来(字符串)

The problem that you are running into is that your object is of type, well, object. In order to evaluate it with string.IsNullOrEmpty, you should pass your object in with the cast to (string)

像这样:

static void Main(string[] args)
{
    object obj = null;

    double d = Convert.ToDouble(string.IsNullOrEmpty((string)obj) ? 0.0 : obj);
    Console.WriteLine(d.ToString());
}

这将很好地工作,因为你没有明确你的(不存在)的对象调用的ToString。

This will work fine since you are not explicitly calling .ToString on your (nonexistent) object.

这篇关于如何检查对象是C#.net 3.5 null或空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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