66

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!

Wouter
  • 1,829
  • 3
  • 28
  • 34
Michael
  • 4,786
  • 11
  • 45
  • 68
  • 8
    @Servy: Just to nitpick on some terminology, **no**, that's an **associative array**. Radu is correct. http://en.wikipedia.org/wiki/Associative_array. Moreover, there are numerous languages in which arrays are not indexed by numbers; JavaScript is one. And while we're criticizing terminology: an **associative array** need not be implemented as a **hash table**. You are confusing an *abstract data type* with its *implementation details*. An associative array might be implemented by balanced binary tree, for example. – Eric Lippert Apr 20 '12 at 16:57

2 Answers2

107

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;
dcp
  • 54,410
  • 22
  • 144
  • 164
  • 2
    Understand that a dictionary does not allow for linear indexing (unless that's how you keyed it), so in the above example, `dict[0]` (an attempt to get the first element of the Dictionary) will not compile. You can work around this several ways; the easiest is to use Linq to enumerate through the KeyValuePair objects of the Dictionary one at a time. Order is not guaranteed, however (or at least it's only guaranteed by virtue of an implementation detail). – KeithS Apr 20 '12 at 17:03
  • 12
    @KeithS There is however an [OrderedDictionary](http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx) if that is required functionality. – Servy Apr 20 '12 at 17:06
  • @KeithS - FWIW, re "at least its only guaranteed by virtue of an implementation detail" - actually, it isn't even that, if you *delete* items, and then add more items - no longer in order. – ToolmakerSteve Mar 08 '19 at 12:58
30

A dictionary will work, but .NET has associative arrays built in. One instance is the NameValueCollection class (System.Collections.Specialized.NameValueCollection).

A slight advantage over dictionary is that if you attempt to read a non-existent key, it returns null rather than throw an exception. Below are two ways to set values.

NameValueCollection list = new NameValueCollection();
list["key1"] = "value1";

NameValueCollection list2 = new NameValueCollection()
{
    { "key1", "value1" },
    { "key2", "value2" }
};
DAG
  • 867
  • 10
  • 16
  • 11
    `NameValueCollection` is essentially a dictionary that is restricted to string for both key and value. IMHO its better to learn to use `Dictionary`, as then you can use it both for strings, and for other objects. Saying "A dictionary will work, but .NET has associative arrays built in" -- implying that `Dictionary` is *not* an associative array -- is nonsensical. Dictionary is just as much as associative array as NameValueCollection. – ToolmakerSteve Mar 08 '19 at 12:52