IsNullOrEmpty with an object

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

obj1 != null 

but not sure if there is another way ...

+6
source share
6 answers

the empty string is null, the empty string is ""

isNullOrEmpty requires a deep understanding of string implementation. If you want, you can write it yourself for your object, but you need to make your own determination whether your object is "empty" or not.

ask yourself: what does it mean that the object is empty?

+9
source
 obj1 != null 

- the right way.

The string defines IsNullOrEmpty as the best way to say

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

therefore, he does more than just check for absence.

There may be other classes that define a method for checking the semantically "empty or null" object, but this will depend on the semantics of the class and by no means universally.

It is also possible to create an extension method to do this 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; } 
+7
source

I found that the values โ€‹โ€‹of the DataGridViewTextBox and some JSON objects are not Null, but are the values "{}" . Comparing them to Null does not work, but using them, do the trick:

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

The good excerpt I found regarding the difference between Null and DBNull:

Do not confuse the concept of null in an object-oriented programming language with the DBNull object. In an object-oriented programming language, null means no reference to the object. DBNull is an uninitialized variant or non-existent database column.

You can learn more about the DBNull class here .

+5
source

The following code works fine and the correct path (more precisely, concise and clear) to check if the object is null :

 object obj = null; //... if (obj == null) { // Do something } 

String.IsNullOrEmpty is a convenient method, so you do not need to write the comparison code yourself:

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

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

+1
source

Why do you need any other way? Comparing an Object reference with null is the least reliable way to check if it is null.

0
source

IsNullOrEmpty is essentially a shorthand for the following:

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

So, there is no function that simply checks for zeros, because that would be too easy. obj != null is the right way. But you can create such a (extra) function yourself using the following extension:

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

Then you can run anyObject.IsNull() .

0
source

Source: https://habr.com/ru/post/906160/


All Articles