在C#中词典数组 [英] Array of dictionaries in C#

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

问题描述

我想用这样的:

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

但是,当我做的:

But, when I do:

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天全站免登陆