Click here to Skip to main content
13,148,427 members (61,702 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 2-Nov-11 5:24am
Updated 2-Nov-11 5:29am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170924.2 | Last Updated 2 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100