Can array indexes be named in C#?

In an array, no. However, there is the very useful Dictionary class, which is a collection of KeyValuePair objects. It's similar to an array in that it is an iterable collection of objects with keys, but more general in that the key can be any type.

Example:

Dictionary<string, int> HeightInInches = new Dictionary<string, int>();
HeightInInches.Add("Joe", 72);
HeightInInches.Add("Elaine", 60);
HeightInInches.Add("Michael", 59);

foreach(KeyValuePair<string, int> person in HeightInInches)
{
    Console.WriteLine(person.Key + " is " + person.Value + " inches tall.");
}

MSDN Documentation for Dictionary<TKey, TValue>


PHP blends the concept of arrays and the concept of dictionaries (aka hash tables, hash maps, associative arrays) into a single array type.

In .NET and most other programming environments, arrays are always indexed numerically. For named indices, use a dictionary instead:

var dict = new Dictionary<string, string> {
    { "foo", "some foo value" }, 
    { "bar", "some bar value" }
};

Unlike PHP's associative arrays, dictionaries in .NET are not sorted. If you need a sorted dictionary (but you probably don't), .NET provides a sorted dictionary type.


Look at Hashtable in C#. This is the data structure that does what you want in C#.