If (Array.Length == 0)

后端 未结 12 1964
耶瑟儿~
耶瑟儿~ 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: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)
    

提交回复
热议问题