C#中的字典数组 [英] Array of dictionaries in C#

查看:96
本文介绍了C#中的字典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用这样的东西:

Dictionary<int, string>[] matrix = new Dictionary<int, string>[2];

但是,当我这样做时:

matrix[0].Add(0, "first str");

它抛出'TargetInvocationException'...异常已被调用目标抛出."

It throws " 'TargetInvocationException '...Exception has been thrown by the target of an invocation."

有什么问题?我是否正确使用了该字典数组?

What is the problem? Am I using that array of dictionaries correctly?

推荐答案

试试这个:

Dictionary<int, string>[] matrix = new Dictionary<int, string>[] 
{
    new Dictionary<int, string>(),
    new Dictionary<int, string>()
};

您需要先实例化数组中的字典,然后才能使用它们.

You need to instantiate the dictionaries inside the array before you can use them.

这篇关于C#中的字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆