If (Array.Length == 0)

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

    Since .Net >= 5.0 the best way is to use Any:

    if(!array.Any()) {
       //now you sure it's empty
    }
    

    For nullable arrays:

    if(!(array?.Any() == true)) {
       //now you sure it's null or empty
    }
    

提交回复
热议问题