C# 检查数组是否为null或空?
我对这行代码有一些问题:C# 检查数组是否为null或空?,c#,C#,我对这行代码有一些问题: if(String.IsNullOrEmpty(m_nameList[index])) 我做错了什么 编辑:在VisualStudio中,m_名称列表用红色下划线,并显示“名称‘m_名称列表’在当前上下文中不存在” 编辑2:我添加了更多的代码 class SeatManager { // Fields private readonly int m_totNumOfSeats; // Constructor public Sea
if(String.IsNullOrEmpty(m_nameList[index]))
我做错了什么
编辑:在VisualStudio中,m_名称列表用红色下划线,并显示“名称‘m_名称列表’在当前上下文中不存在”
编辑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;
}
}
}
classseatmanager
{
//田地
私人只读int m_TotnumoSeats;
//建造师
公共座位管理器(int maxNumOfSeats)
{
m_totNumOfSeats=maxmumofseats;
//为名称和价格创建数组
string[]m_nameList=新字符串[m_totNumOfSeats];
double[]m_priceList=新的double[m_totNumOfSeats];
}
public int GetNumReserved()
{
int TOTALAMONONTRESERVED=0;
对于(int index=0;index如果m_nameList
为空,则仍会爆炸,因为它将尝试查找要传递给String.IsNullOrEmpty的元素。您需要:
if (m_nameList == null || String.IsNullOrEmpty(m_nameList[index]))
这也是假设如果m_nameList
为非空,index
将是有效的
当然,这是检查数组的元素是否为null或空,或者数组引用本身是否为null。如果只想检查数组本身(如标题所示),则需要:
if (m_nameList == null || m_nameList.Length == 0)
编辑:现在我们可以看到您的代码,有两个问题:
- 正如Henk在他的回答中所显示的,当您需要一个字段时,您试图使用一个局部变量
- 由于以下原因,您还将获得一个
ArrayIndexOutOfBoundsException
(一旦您使用了一个字段):
for (int index = 0; index <= m_totNumOfSeats; index++)
请注意,m_nameList[m_totNumOfSeats]
无效,因为数组索引
从C#中的0开始。因此,对于5个元素的数组,有效索引为0、1、2、3、4
GetNumReserved
方法的另一个选项是使用:
int count = 0;
foreach (string name in m_nameList)
{
if (string.IsNullOrEmpty(name))
{
count++;
}
}
return count;
或者使用LINQ,它是一个单行程序:
return m_nameList.Count(string.IsNullOrEmpty);
(但你确定你没有弄错吗?我本以为预订应该是名称不为null或空的预订,而不是名称为null或空的预订。)
如果方向不对,那么在LINQ中应该是这样:
return m_nameList.Count(name => !string.IsNullOrEmpty(name));
为了避免错误,您可以在if中执行一些先决条件,例如:
if(m_nameList == null || index < 0 || m_nameList.Length < index || String.IsNullOrEmpty(m_nameList[index]))
if(m|u nameList==null | | index<0 | | m|u nameList.Length
这在几乎任何情况下都可以正常工作(不会导致错误…编辑2后:
您正在将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];
}
....
}
什么问题?你有IndexOutOfRangeException吗?发布你的错误那是写marlon…收回我的评论很好。那里有很多测试…我数了七次?不,八次!让我觉得异常确实很好!这是为了避免异常,它不应该比获得异常更快。@KevinP.Rice:我永远不会我们必须让异常通过并在这里捕获它。ArrayIndexOutOfBounds
异常应该总是指示一个bug,而不是故意的。我统计了四个测试(当然是全部测试或合并测试),不是8个。这里有4个条件被检查,所有条件都可以检查。你把每个复合条件都算为一个额外的条件吗?@KevinP.Rice:我想任何不想在这里得到最大可能数的人都会把它算为4个条件。我想问题的标题是“检查数组是否为null或空?”假设OP确实想要执行检查是合理的。当然,让异常冒泡出来可能没问题——但这不是您的建议。您建议捕获异常:“如果罕见,那么try/catch更好。”“。我绝对不同意。捕获ArrayIndexOutOfBoundsException或NullReferenceException是个坏主意。@KevinP.Rice:今晚不行,但我明天会发邮件调查。太好了!我错过了!谢谢你的帮助!”)@3D Kretiv:注意,这只是代码中的一个问题-另一个问题请参见我的答案。@Downvoter:要不要评论?请记住,这个答案是在OP包含他的代码之前很久写的…感谢您的帮助!是的,代码现在是错误的方式raound!我会修复它!
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];
}
....
}