如果(Array.Length == 0) [英] If (Array.Length == 0)
问题描述
如果数组为空,则似乎无法使用".length"检查其长度.检查数组是否为空的最佳方法是什么?
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?
推荐答案
您可以绝对检查空数组的长度.但是,如果您尝试对null引用执行此操作,则会得到异常.我怀疑这就是您遇到的问题.您可以同时解决这两个问题:
You can absolutely check an empty array's length. However, if you try to do that on a null reference you'll get an exception. I suspect that's what you're running into. You can cope with both though:
if (array == null || array.Length == 0)
如果不是这个原因,请给出一个简短但完整的程序来说明问题.如果是 原因,值得花一点时间来确保您了解空引用与空"集合/字符串/任何内容.
If that isn't the cause, please give a short but complete program demonstrating the problem. If that was the cause, it's worth taking a moment to make sure you understand null references vs "empty" collections/strings/whatever.
这篇关于如果(Array.Length == 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!