[c#] IsNullOrEmpty with Object

IsNullOrEmpty is used with strings to check if a string is null or empty. Is there an equivalent with an object to see if any object is null or not? I assume we can do

    obj1 != null 

but not sure if there is any other way...

This question is related to c#

The answer is


Why would you need any other way? Comparing an Object reference with null is the least-verbose way to check if it's null.


obj1 != null  

is the right way.

String defines IsNullOrEmpty as a nicer way to say

obj1 == null || obj == String.Empty

so it does more than just check for nullity.

There may be other classes that define a method to check for a sematically "blank or null" object, but that would depend on the semantics of the class, and is by no means universal.

It's also possible to create extension method to do this kind of thing if it helps the readability of your code. For example, a similar approach to collections:

public static bool IsNullOrEmpty (this ICollection collection)
{
    return collection == null || collection.Count == 0;
}

I found that DataGridViewTextBox values and some JSON objects don't equal Null but instead are "{}" values. Comparing them to Null doesn't work but using these do the trick:

if (cell.Value is System.DBNull)

if (cell.Value == System.DBNull.Value)

A good excerpt I found concerning the difference between Null and DBNull:

Do not confuse the notion of null in an object-oriented programming language with a DBNull object. In an object-oriented programming language, null means the absence of a reference to an object. DBNull represents an uninitialized variant or nonexistent database column.

You can learn more about the DBNull class here.


The following code is perfectly fine and the right way (most exact, concise, and clear) to check if an object is null:

object obj = null;

//...

if (obj == null)
{
    // Do something
}

String.IsNullOrEmpty is a method existing for convenience so that you don't have to write the comparison code yourself:

private bool IsNullOrEmpty(string input)
{
    return input == null || input == string.Empty;
}

Additionally, there is a String.IsNullOrWhiteSpace method checking for null and whitespace characters, such as spaces, tabs etc.


You may be checking an object null by comparing it with a null value but when you try to check an empty object then you need to string typecast. Below the code, you get the idea.

if(obj == null || (string) obj == string.Empty)
{
   //Obj is null or empty
}

object MyObject = null;

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


IsNullOrEmpty is essentially shorthand for the following:

return str == null || str == String.Empty;

So, no there is no function that just checks for nulls because it would be too simple. obj != null is the correct way. But you can create such a (superfluous) function yourself using the following extension:

public bool IsNull(this object obj)
{
    return obj == null;
}

Then you are able to run anyObject.IsNull().


You can simply compare it with System.DBNull.Value