Ozzy
Ozzy

Reputation: 10643

C# PHP style array keys

I have a string array:

String[] array1 = new String[10];

Is there anyway I can use keys which are nto numbers?

array["keyhere"] instead of array1[1]

anyone know how?

Upvotes: 3

Views: 889

Answers (3)

Malfist
Malfist

Reputation: 31805

PHP arrays are called associative arrays. You can use either a Dictionary or HashMap to implement the same thing in C#.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

Use a dictionary

Dictionary<String,Object> phpArray = new Dictionary<String,Object>();
phpArray.Add("keyhere",1);
MessageBox.Show(phpArray["keyhere"]);

Upvotes: 1

Donut
Donut

Reputation: 112845

Use System.Collections.Generic.Dictionary<TKey, TValue>.

For example:

Dictionary<string, string> myDictionary = new Dictionary<string, string>();

myDictionary.Add("key", "value");

string foo = myDictionary["key"];

Dictionary<TKey, TValue> has some methods that you might find useful, such as ContainsKey().

Upvotes: 7

Related Questions