c# - 使用实际上是列表的类对象数组
问题描述
我需要弄清楚如何声明一个数组,并且数组中的每个对象都是另一个对象的列表。
所以结构会是这样的:
myArray[0] = List<MyObject>();
myArray[1] = List<MyObject>();
MyObject 有 3 个部分:
int anId;
string firstName;
string LastName;
我该如何声明并正确初始化它?我对其他想法持开放态度。
解决方案
你可以这样做:
var items = new List<MyObject>[4];
items[0] = new List<MyObject>();
items[1] = new List<MyObject>();
items[2] = new List<MyObject>();
items[3] = new List<MyObject>();
或这个:
List<MyObject>[] items = {
new List<MyObject>(),
new List<MyObject>(),
new List<MyObject>(),
new List<MyObject>()
};
但我想知道你是否真的想要这个:
var items = new List<List<MyObject>>();
items.Add(new List<MyObject>);
items.Add(new List<MyObject>);
items.Add(new List<MyObject>);
items.Add(new List<MyObject>);
推荐阅读
- c - 是否可以在 Linux 中反汇编正在运行的进程?
- c++ - 在类成员函数中使用双下划线作为参数名
- excel - 识别星期几和时间限制并循环遍历范围
- python - 如何在 Hangman 游戏中进行字母检测?
- sql - 如何在连接到 Redshift 的 DBeaver 中使用 SQL 连接 2 列
- c# - 试图将对象的变量传递给另一个方法
- alexa - 如何将渐变应用到 AlexaHeadline 模板
- python - 谁能告诉我 %.1f 是什么意思
- firebase - Flutter & Firebase:如何检查某个项目是否存在于 Firebase 文档中的数组中
- docker - docker新手:nginx退出代码1 wp退出代码0,没有SSL