如何初始化字典列表 [英] how to Initialize a dictionary list

查看:372
本文介绍了如何初始化字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dictionary<string,> newList = new Dictionary<string,>();
newList.add("cat", "dog");


从上面的代码中,我试图初始化字典列表.在我的情况下,我将它传递给一个函数,该函数将被填充并返回.

有时它会返回为emty列表,因此当我使用它时,它会抛出null异常.

所以我想在将列表传递给函数之前初始化列表.为此,我在列表中添加了两个值(假设它已初始化),但没有初始化.

您能给我正确的初始化代码吗?

如果我在通过之前进行初始化,它将解决问题还是在哪里初始化?


From the above code I''m trying to initialize the dictionary list. coz in my case I pass it to a function where it get filled and comes back.

Some times it comes back as emty list so when i use it, its throwing null exception.

So I want to initialize the list before i pass it to the function. To do that I added two values to the list assuming its an initialization, but its not.

Could you please give me the right code for initialization.

If I initialize before a pass it, would it solve the problem or where should i initialize it?

推荐答案

一旦调用new ,它已经初始化.您可能会看到的是,假设该函数在集合中至少填充了n个条目,并且当您盲目访问这些元素时,您将遇到null异常.确保检查计数,密钥是否存在等.如果仍然遇到问题,请发布更多代码(最少复制),然后有人会为您提供帮助.
Once you call new it''s already initialized. What you are probably seeing is that you are assuming that the function populates at least n entries into the collection, and when you access those elements blindly, you will run into null exceptions. Make sure you check for things like count, whether a key exists etc. And if you still have trouble, post more code (minimal repro) and someone''ll be able to help you out.


这篇关于如何初始化字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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