What is the easiest way to handle associative array in c#?

Michael picture Michael · Apr 20, 2012 · Viewed 73.8k times · Source

I do not have a lot of experience with C#, yet I am used of working with associative arrays in PHP.

I see that in C# the List class and the Array are available, but I would like to associate some string keys.

What is the easiest way to handle this?

Thx!

Answer

dcp picture dcp · Apr 20, 2012

Use the Dictionary class. It should do what you need. Reference is here.

So you can do something like this:

IDictionary<string, int> dict = new Dictionary<string, int>();
dict["red"] = 10;
dict["blue"] = 20;