我有一个二维数组或结构体。
那么,我该如何检查密钥对是否存在呢?
public struct MapCell
{
public string tile;
}
public MapCell[,] worldMap;
但是没有办法检查这个数组中是否存在密钥对...没有相关的方法可用。
我尝试像这样做
if (worldMap[tileX, tileY] != null) {
}
它不起作用:
Error 1 Operator '!=' cannot be applied to operands of type 'Warudo.MapCell' and '<null>'
并且为了
if (worldMap[tileX, tileY].tile != null) {
它也无法工作(当它遇到不存在的元素时,异常会弹出)。
Index was outside the bounds of the array.
那么,我该如何检查密钥对是否存在呢?
worldMap
的?你发布的代码不应该抛出异常,除非你没有初始化worldMap
或者你试图访问数组超出边界的索引,但是你的if
条件应该总是为真,因为一个struct
永远不可能等于null
。 - Steven Oxley