If (Array.Length == 0)

If the array is empty, it looks like you cannot check its length using ".length". What is the best way to check if an array is empty?

+54
arrays c #
Jul 09 2018-10-09T00:
source share
12 answers

You can absolutely check the length of an empty array. However, if you try to do this with a null reference, you will get an exception. I suspect you are facing. You can handle both, though:

if (array == null || array.Length == 0) 

If this is not the case, please give a short but complete program demonstrating the problem. If that was the reason, it's worth taking a little time to make sure you understand null references versus "empty" collections / strings / whatever.

+137
Jul 09 2018-10-14T00:
source share

Yes, for security, I would probably do:

 if(array == null || array.Length == 0) 
+13
Jul 09 2018-10-14T00:
source share

you can use

 if (array == null || array.Length == 0) 

OR

 if (!(array != null && array.Length >= 0)) 

NOTE!!!!! To ensure that C # performs the short circuit correctly; you must compare that object with NULL before proceeding to compare children with the object.

+8
Jul 09 '10 at 17:43
source share

You can use .Length == 0 if the length is empty and the array exists, but are you sure that it is not null?

+4
Jul 09 2018-10-14T00:
source share

This is the best way. Note that Array is an object on NET, so you need to check for null before.

+2
Jul 09 '10 at 14:09
source share

As already stated, you are probably getting a NullReferenceException that you can avoid by first checking to see if there is a null reference. However, you need to ask yourself if this is really a test. Will you do this because the link may indeed be null , and null has special meaning in your code? Or are you doing this to hide a mistake? The nature of the issue makes me believe that this will be the last. In this case, you really need to study the code in detail and find out why this link was not correctly initialized in the first place.

+2
Jul 09 '10 at 14:17
source share

John Skeet answered correctly. Just remember that the order of the test in "IF" is important. Check the null value to the end. I also prefer to put zero on the left side of the equal ... just a habit I got from Java that made the code more efficient and faster ... I don't think this is important in many applications today, but it's good practice.

if (null == array || array.Length == 0)

+2
Jul 09 '10 at 14:21
source share

If array is null , attempting to eliminate array.Length will NullReferenceException . If your code considers null invalid value for array , you must reject it and blame the caller. One of these patterns is to throw an ArgumentNullException :

 void MyMethod(string[] array) { if (array == null) throw new ArgumentNullException(nameof(array)); if (array.Length > 0) { // Do something with array… } } 

If you want to accept the null array as an indication not to do anything or as an optional parameter, you can simply not access it if it is null:

 void MyMethod(string[] array) { if (array != null) { // Do something with array here… } } 

If you want to not touch array when it is either null or has zero length, then you can check both at the same time as C # -6s operator with zero coalescence .

 void MyMethod(string[] array) { if (array?.Length > 0) { // Do something with array… } } 

Excess length check

It seems strange that you are treating an empty array as a special case. In many cases, if you, for example, just go through the array anyway, there is no need to consider an empty array as a special case. foreach (var elem in array) {«body»} just won't execute «body» when array.Length is 0 . If you are referring to array == null || array.Length == 0 array == null || array.Length == 0 specifically, for example, to improve performance, you can leave a comment for posterity. Otherwise, checking for Length == 0 seems unnecessary.

Redundant code complicates the understanding of the program, because people reading the code supposedly assume that each line is necessary to solve a problem or achieve correctness. If you include unnecessary code, readers are going to spend forever trying to figure out why this line was or was needed before deleting it; -).

+2
Jun 27 '17 at 21:52
source share

Your suggested test is ok if the array is inized ...

Martin

+1
Jul 09 '10 at 14:10
source share

if (array.length> 0)

+1
Jul 09 '10 at 16:14
source share

check if the array is first in null, so you avoid a null pointer exception.

logic in any language: if the array is null or empty: do ....

+1
Jul 09 '10 at 18:08
source share

You mean empty or null, two different things,

if the array is created but empty, the length is true, if it was not created, then vs null check

0
Jul 09 '10 at 14:09
source share



All Articles