是否可以将包含列表的类存储在数组中?存储数组中的列表
我对这个概念有些麻烦。
这里是我的代码:
我的班级叫 “arrayItems”:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EngineTest
{
[Serializable] //List olarak save etmemiz için bu gerekli.
public class arrayItems
{
public List<items> items = new List<items>();
}
}
这里是我的数组称为定义 “tileItems”:
public static arrayItems[, ,] tileItems;
以下是我创造我的阵:
Program.tileItems = new arrayItems[Program.newMapWidth, Program.newMapHeight, Program.newMapLayers];
我面临的问题是我的数组的内容为空。当我试图通过添加填充列表的数组内()命令我得到同样的错误
Object reference not set to an instance of an object.
: 我正在此错误。
请问您能指导我正确的方向吗? 在此先感谢。
实例化阵列'titleItems',所以空间'arrayItems'引用已经分配但,你有没有设置为'arrayItems'类的有效实例的引用。 – Jodrell
快速解决方法是将'public class arrayItems'更改为'public struct arrayItems',然后'arrayItems'将是一个值类型,而不是引用类型。但是,这可能会产生其他不希望的副作用。 – Jodrell