Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# have array with index is a string like PHP?

I have been learning C# myself for 2 months. Before, I learned PHP and see that it has an array where the index is a string, like this:

$John["age"] = 21;
$John["location"] = "Vietnam";

It is very useful to remember what we set to an array element. I tried to find if C# supports that array type, but I haven't seen any answers, yet.

Does C# have an array like this? If it does, how can I create it?

like image 924
Thanh Nguyen Avatar asked Sep 12 '12 15:09

Thanh Nguyen


1 Answers

C# supports any type of object for an index. A baked-in implementation is System.Collections.Generic.Dictionary<T1,T2>. You can declare one like this:

Dictionary<string, string> myDictionary = new Dictionary<string, string>();
like image 141
Brian Warshaw Avatar answered Nov 02 '22 22:11

Brian Warshaw