2

Possible Duplicate:
Set array key as string not int?

I know you can do something like $array["string"] in php but can you also do something like this in C# instead of using arrays with numbers?

Community
  • 1
  • 1
repwn
  • 29
  • 2

5 Answers5

9

Arrays in PHP are in reality more like dictionaries in C#. So yes, you can do this, using a Dictionary<string, YourValueType>:

var dict = new Dictionary<string, int>();
dict["hello"] = 42;
dict["world"] = 12;
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
1

If you want to store key/value pairs, you could probably use a Dictionary: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

In PHP the "array" class is actually a map, which is a data structure that maps values to keys.

In C# there are a lot of distinct data structure classes. Each with it's own characteristics.
Wikipedia is a good starting point to read about basic data structures in general: http://en.wikipedia.org/wiki/Data_structure#Common_data_structures

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
0

var ar = new string[]{"one", "two"};

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
Xhalent
  • 3,914
  • 22
  • 21
0

Use Dictionary for this purpose :)

Igoris Azanovas
  • 1,640
  • 1
  • 22
  • 37
0
var dictionary = new Dictionary<string, string>();
dictionary["key"] = "value";

and so on

genesis
  • 50,477
  • 20
  • 96
  • 125