Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Dictionary<string,> newList = new Dictionary<string,>();
newList.add("cat", "dog");

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?
Posted
Updated 2-Nov-11 5:29am
v2

1 solution

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.
 
Share this answer
 
v2
Comments
hzawary 2-Nov-11 11:34am    
Sorry, may you see problem me?
The "How to resizing a large image of ImageBrush object to small under WPF?" in Questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900