Reputation: 25
I used to create this kind of arrays in PHP:
$myarray[0]["my_string_1"] = "Toto";
$myarray[0]["my_string_2"] = "Tata";
$myarray[0]["my_int_1"] = 25;
$myarray[0]["my_int_2"] = 28;
$myarray[1]["my_string_1"] = "Titi";
$myarray[1]["my_string_2"] = "Tutu";
$myarray[1]["my_int_1"] = 12;
$myarray[1]["my_int_2"] = 15;
My question is: Is it possible to do the same thing in C#?
Upvotes: 0
Views: 160
Reputation: 2124
Not familiar with PHP but it looks like your array has a 'map' of string to string or int. This can be done in C# using a Dictionary and Dynamic:
var myarray = new Dictionary<int, Dictionary<string, dynamic>>()
{
{
0, new Dictionary<string, dynamic>()
{
{"my_string_1", "Toto"},
{"my_string_2", "Tata"},
{"my_int_1", 25},
{"my_int_2", 28},
}
},
{
1, new Dictionary<string, dynamic>()
{
{"my_string_1", "Titi"},
{"my_string_2", "Tutu"},
{"my_int_1", 12},
{"my_int_2", 15},
}
}
};
See the documentation for more info on Dictionary and Dynamic.
Edit:
As others have suggested, you might be better off using:
var myarray = new Dictionary<int, Dictionary<string, object>>()
And instead of using a dictionary with an int
as the key, an actual array could be used:
var myarray = new Dictionary<string, object>[2];
myarray[0] = new Dictionary<string, object>
{
{"my_string_1", "Toto"},
{"my_string_2", "Tata"},
{"my_int_1", 25},
{"my_int_2", 28},
};
myarray[1] = new Dictionary<string, object>
{
{"my_string_1", "Titi"},
{"my_string_2", "Tutu"},
{"my_int_1", 12},
{"my_int_2", 15},
};
Upvotes: 1