C# 检查数组是否为空或空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10052914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if array is null or empty?
提问by 3D-kreativ
I have some problem with this line of code:
我对这行代码有一些问题:
if(String.IsNullOrEmpty(m_nameList[index]))
What have I done wrong?
我做错了什么?
EDIT: The m_nameList is underlined with red color in VisualStudio, and it says "the name 'm_nameList' does not exist in the current context"??
编辑:m_nameList 在 VisualStudio 中带有红色下划线,它说“当前上下文中不存在名称‘m_nameList’”??
EDIT 2: I added some more code
编辑 2:我添加了更多代码
class SeatManager
{
// Fields
private readonly int m_totNumOfSeats;
// Constructor
public SeatManager(int maxNumOfSeats)
{
m_totNumOfSeats = maxNumOfSeats;
// Create arrays for name and price
string[] m_nameList = new string[m_totNumOfSeats];
double[] m_priceList = new double[m_totNumOfSeats];
}
public int GetNumReserved()
{
int totalAmountReserved = 0;
for (int index = 0; index <= m_totNumOfSeats; index++)
{
if (String.IsNullOrEmpty(m_nameList[index]))
{
totalAmountReserved++;
}
}
return totalAmountReserved;
}
}
}
采纳答案by Henk Holterman
After Edit2:
Edit2之后:
You are defining m_nameListas a local variable of the constructor.
The rest of your code needs it as a field:
您正在定义 m_nameList为构造函数的局部变量。
其余代码需要将其作为字段:
class SeatManager
{
// Fields
private readonly int m_totNumOfSeats;
private string[] m_nameList;
private double[] m_priceList;
// Constructor
public SeatManager(int maxNumOfSeats)
{
m_totNumOfSeats = maxNumOfSeats;
// Create arrays for name and price
m_nameList = new string[m_totNumOfSeats];
m_priceList = new double[m_totNumOfSeats];
}
....
}
回答by Jon Skeet
If m_nameListis null, that will still blow up, because it will try to find the element to pass to String.IsNullOrEmpty. You'd want:
如果m_nameList为空,那仍然会爆炸,因为它会尝试找到要传递给的元素String.IsNullOrEmpty。你会想要:
if (m_nameList == null || String.IsNullOrEmpty(m_nameList[index]))
That's alsoassuming that indexis going to be valid if m_nameListis non-null.
这也假设index如果m_nameList非空,它将是有效的。
Of course, this is checking if the elementof an array is null or empty, or if the array reference itself is null. If you justwant to check the array itself (as your title suggests) you want:
当然,这是检查数组的元素是否为空或为空,或者数组引用本身是否为空。如果您只想检查数组本身(如您的标题所示),您需要:
if (m_nameList == null || m_nameList.Length == 0)
EDIT: Now we can see your code, there are twoproblems:
编辑:现在我们可以看到你的代码,有两个问题:
- As Henk showed in his answer, you're trying to use a localvariable when you need a field
You're alsogoing to get an
ArrayIndexOutOfBoundsException(once you've used a field) due to this:for (int index = 0; index <= m_totNumOfSeats; index++)That will perform
m_totNumOfSeats + 1iterations because of your bound. You want:for (int index = 0; index < m_totNumOfSeats; index++)Note that
m_nameList[m_totNumOfSeats]is notvalid, because array indexes start at 0 in C#. So for an array of 5 elements, the valid indexes are 0, 1, 2, 3, 4.
- 正如 Henk 在他的回答中所示,当您需要一个字段时,您正在尝试使用局部变量
由于这个原因,您还将获得一个
ArrayIndexOutOfBoundsException(一旦您使用了一个字段):for (int index = 0; index <= m_totNumOfSeats; index++)m_totNumOfSeats + 1由于您的限制,这将执行迭代。你要:for (int index = 0; index < m_totNumOfSeats; index++)请注意,
m_nameList[m_totNumOfSeats]是不是有效的,因为数组下标0在C#中开始。因此,对于 5 个元素的数组,有效索引为 0、1、2、3、4。
Another option for your GetNumReservedmethod would be to use:
您的GetNumReserved方法的另一种选择是使用:
int count = 0;
foreach (string name in m_nameList)
{
if (string.IsNullOrEmpty(name))
{
count++;
}
}
return count;
Or using LINQ, it's a one-liner:
或者使用 LINQ,它是单行的:
return m_nameList.Count(string.IsNullOrEmpty);
(Are you sure you haven't got it the wrong way round though? I would have thought reservations would be the ones where the name isn'tnull or empty, not the ones where it isnull or empty.)
(你确定你没有把这个圆圆的错误呀?我本来以为保留将是那些在名称不是null或空,不是那些地方是空。)
If it's the wrong way round, it would be this instead in LINQ:
如果方法不对,则在 LINQ 中会是这样:
return m_nameList.Count(name => !string.IsNullOrEmpty(name));
回答by aleroot
To avoid the error you can perform some pre conditions in the if, like these :
为了避免错误,您可以在 if 中执行一些前置条件,如下所示:
if(m_nameList == null || index < 0 || m_nameList.Length < index || String.IsNullOrEmpty(m_nameList[index]))
This should works fine(without causing error) in almost any conditions ...
这应该在几乎任何情况下都可以正常工作(不会导致错误)......

