C#中的字典数组
我想用这样的东西:
Dictionary[] matrix = new Dictionary[2];
但是,当我这样做时:
matrix[0].Add(0, "first str");
它抛出“’TargetInvocationException’…调用的目标抛出了exception。”
问题是什么? 我正确使用那个字典数组吗?
试试这个:
Dictionary[] matrix = new Dictionary[] { new Dictionary(), new Dictionary() };
您需要在使用它们之前实例化数组中的字典。
您是否将数组对象设置为Dictionary实例?
Dictionary[] matrix = new Dictionary[2]; matrix[0] = new Dictionary(); matrix[1] = new Dictionary(); matrix[0].Add(0, "first str");
Dictionary[] matrix = new Dictionary[2];
这样做会分配数组’matrix’,但是那个应该包含在该数组中的字典永远不会被实例化。 您必须使用new关键字在数组的所有单元格中创建一个Dictionary对象。
matrix[0] = new Dictionary(); matrix[0].Add(0, "first str");
你已经初始化了数组,但没有初始化字典。 您需要初始化matrix [0](尽管这应该导致空引用exception)。
你忘了初始化词典。 在添加项目之前,请在下面添加以下行:
上述就是C#学习教程:C#中的字典数组分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
matrix[0] = new Dictionary();
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/990410.html