If (Array.Length == 0)

后端 未结 12 1911
耶瑟儿~
耶瑟儿~ 2020-12-23 11:23

If an array is empty, it looks like you can\'t check it\'s length using \".length\". What\'s the best way to check if an array is empty?

相关标签:
12条回答
  • 2020-12-23 11:45

    Yeah, for safety I would probably do:

    if(array == null || array.Length == 0)
    
    0 讨论(0)
  • 2020-12-23 11:53

    You can use

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

    OR

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

    NOTE!!!!! To insure that c# will implement the short circuit correctly; you have to compare that the object with NULL before you go to the children compare of the object.

    C# 7.0 and above

    if(!(array?.Length != 0))
    
    0 讨论(0)
  • 2020-12-23 11:55

    If array is null, trying to derefrence array.Length will throw a NullReferenceException. If your code considers null to be an invalid value for array, you should reject it and blame the caller. One such pattern is to throw 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 a null array as an indication to not do something or as an optional parameter, you may simply not access it if it is null:

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

    If you want to avoid touching array when it is either null or has zero length, then you can check for both at the same time with C#-6’s null coalescing operator.

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

    Superfluous Length Check

    It seems strange that you are treating the empty array as a special case. In many cases, if you, e.g., would just loop over the array anyway, there’s no need to treat the empty array as a special case. foreach (var elem in array) {«body»} will simply never execute «body» when array.Length is 0. If you are treating array == null || array.Length == 0 specially to, e.g., improve performance, you might consider leaving a comment for posterity. Otherwise, the check for Length == 0 appears superfluous.

    Superfluous code makes understanding a program harder because people reading the code likely assume that each line is necessary to solve some problem or achieve correctness. If you include unnecessary code, the readers are going to spend forever trying to figure out why that line is or was necessary before deleting it ;-).

    0 讨论(0)
  • 2020-12-23 11:56

    This is the best way. Please note Array is an object in NET so you need to check for null before.

    0 讨论(0)
  • 2020-12-23 11:56

    check if the array is null first so you would avoid a null pointer exception

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

    0 讨论(0)
  • 2020-12-23 11:57

    Jon Skeet answered correctly. Just remember that the order of the test in the "IF" is important. Check for the null before the length. I also prefer to put the null on the left side of the equal which is a habit I got from Java that made the code more efficient and fast… I don't think it's important in a lot of application today, but it's a good practice!

    if (null == array || array.Length == 0)
    
    0 讨论(0)
提交回复
热议问题