如何在 C#.NET 3.5 中检查对象是否为 null 或为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9323580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check object is null or empty in C#.NET 3.5?
提问by venkat
If objects contains null or empty then how to validate or check the condition for the same?
如果对象包含 null 或空,那么如何验证或检查相同的条件?
How to bool check whether object objis nullor Empty
如何布尔检查对象obj是null或Empty
I've code as follows:
我的代码如下:
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());
}
}
With this code i'm getting NullReference Exceptionas Object reference not set to an instance of an object.
有了这个代码,我得到NullReference Exception了 Object reference not set to an instance of an object.
Pls help.
请帮忙。
Here I'm not getting....
在这里,我不明白......
How to validate whether that object is nullor Emptywithout converting into .ToString() ??
如何验证对象是否null或Empty不转换成的ToString()?
Is there an approach to check the same??
有没有办法检查相同的?
采纳答案by Stefan H
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)
您遇到的问题是您的对象是类型,好吧,对象。为了使用 string.IsNullOrEmpty 对其进行评估,您应该将您的对象与强制转换传递给 (string)
like so:
像这样:
static void Main(string[] args)
{
object obj = null;
double d = Convert.ToDouble(string.IsNullOrEmpty((string)obj) ? 0.0 : obj);
Console.WriteLine(d.ToString());
}
This will work fine since you are not explicitly calling .ToString on your (nonexistent) object.
这将正常工作,因为您没有在(不存在的)对象上显式调用 .ToString 。
回答by TBohnen.jnr
You are getting the null reference because you are executing obj.ToString()which will return obj's ToString() method return value. Problem is that in the previous line you set obj to null so you will get an object reference not... error
您正在获取空引用,因为您正在执行obj.ToString()它将返回 obj 的 ToString() 方法返回值。问题是,在上一行中,您将 obj 设置为 null,因此您将获得一个对象引用,而不是...错误
To make your code work you need to do:
要使您的代码正常工作,您需要执行以下操作:
//This will check if it's a null and then it will return 0.0 otherwise it will return your obj.
double d = Convert.ToDouble(obj ?? 0.0);
Your code as it is now however will always be 0.0
您现在的代码将始终为 0.0
Without null coalescing: (??)
没有空合并:(??)
double d = Convert.ToDouble(obj ? 0.0 : obj);
EDIT
编辑
If I understand correctly from the comments you want to know if the object is null or an empty string. You can do this by casting it to string first instead of calling the ToStringmethod which does something entirely different:
如果我从评论中正确理解您想知道对象是 null 还是空字符串。您可以先将其强制转换为字符串,而不是调用执行完全不同的操作的ToString方法来完成此操作:
string objString = (obj as string);
double d = Convert.ToDouble(string.IsNullOrEmpty(objString) ? "0.0" : objString);
回答by Adam Mihalcin
You shouldn't be a bit surprised that you get a NullReferenceExceptionwith this code. The offending part is
NullReferenceException使用此代码获得 a 时,您应该不会感到惊讶。违规部分是
obj.ToString()
If you wrote
如果你写
object obj = null;
string s = obj.ToString();
you would expect a NullReferenceException. Since the call to ToStringoccurs before the call to string.IsNullOrEmpty, the exception is thrown before there is a check for a null or empty string.
你会期望一个NullReferenceException. 由于对 的调用ToString发生在对 的调用string.IsNullOrEmpty之前,因此在检查空字符串或空字符串之前抛出异常。
回答by Mohit Vashistha
You can use the ??operator. It is known as the null-coalescingoperator.
您可以使用??运算符。它被称为空合并运算符。
回答by Dmitry S.
It sounds like what you want to do is this:
听起来你想要做的是:
object obj = null;
double d;
if (!double.TryParse(Convert.ToString(obj), out d))
{
d = 0.0;
}
But the question does not make a lot of sense.
但这个问题没有多大意义。
回答by user3245067
class Program
{
static void Main(string[] args)
{
object obj = DBNull.Value;
if(obj != DBNull.Value) {
double d = Convert.ToDouble(obj);
Console.WriteLine(d.ToString());
}
}
}
回答by Praveen Kumar Thalluri
Following code could be a safer way of achieving it.
以下代码可能是实现它的更安全的方法。
if(obj != null && !string.IsNullOrEmpty(obj.ToString()))
{
}
This code saves us from object being a non-string type.
这段代码使我们免于对象是非字符串类型。

