How to check an object empty or empty in C # .NET 3.5?

If the objects contain null or empty, then how to check or check the condition for the same?

How to bool check if obj object is null or Empty

I have the 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 get a NullReference Exception as Object reference not set to an instance of an object.

Help Pls.

Here I do not get ....

How to check if this object is null or Empty without conversion to .ToString () ??

Is there a way to check the same?

+7
source share
7 answers

The problem you are facing is that your object is of type, well, an object. To evaluate it with string.IsNullOrEmpty, you have to pass your object with cast to (string)

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 just fine, as you are not explicitly calling .ToString on your (non-existent) object.

+12
source

You get a null reference because you are running obj.ToString() , which returns the return value of the obj ToString () method. The problem is that in the previous line you set obj to null to get a reference to the object not ... error

To do the work with the code, you need to do:

 //This will check if it a null and then it will return 0.0 otherwise it will return your obj. double d = Convert.ToDouble(obj ?? 0.0); 

Now your code, as now, will always be 0.0

Without zero coalescence: (??)

 double d = Convert.ToDouble(obj ? 0.0 : obj); 

EDIT

If I understand correctly from the comments you want to know if the object is empty or empty. You can do this by setting the string first, rather than calling the ToString method, which does something completely different:

 string objString = (obj as string); double d = Convert.ToDouble(string.IsNullOrEmpty(objString) ? "0.0" : objString); 
+3
source
 class Program { static void Main(string[] args) { object obj = DBNull.Value; if(obj != DBNull.Value) { double d = Convert.ToDouble(obj); Console.WriteLine(d.ToString()); } } } 
+2
source

Can you use the operator ?? . It is known as the null-coalescing operator .

+1
source

It looks like you want to do this:

 object obj = null; double d; if (!double.TryParse(Convert.ToString(obj), out d)) { d = 0.0; } 

But the question does not make much sense.

+1
source

You should not be surprised that with this code you get a NullReferenceException . Breaking part

 obj.ToString() 

If you wrote

 object obj = null; string s = obj.ToString(); 

you expect a NullReferenceException . Since the ToString call occurs before the call to string.IsNullOrEmpty , an exception is thrown before checking for a null or empty string.

0
source

The following code may be a safer way to achieve it.

 if(obj != null && !string.IsNullOrEmpty(obj.ToString())) { } 

This code saves us from the fact that the object is non-string.

-one
source

All Articles