gomesh munda
gomesh munda

Reputation: 850

PHP equivalent jagged arrays in C#

In PHP5 i have declared some arrays as

$secArray0 = array();
$secArray1 = array();
$secArray2 = array();
$secArray3 = array();
$secArray = array();

Later on some where in the code I insert key-value pair in the above arrays as

    $pHolder0 = array(
    'Name'   => 'Gomesh',
    'EmpCode'   => 'ID04',
    'DeptId'     => '1C',
    'Age'=> '25'
);
array_push($secArray0,$pHolder0);

And so on for $secArray1, $secArray2, $secArray3.

And finally I insert $secArray0,$secArray1....$secArray3 inside $secArray as

$secArray = array($secArray0,$secArray1,$secArray2,$secArray3);

Now my question is can I accomplish such thing in C#?

So far I have done..

var secArray0 = new List<KeyValuePair<string, string>>();
secArray0.Add(new KeyValuePair<string, string>("Name", "gomesh"));
secArray0.Add(new KeyValuePair<string, string>("EmpCode", "ID04"));
secArray0.Add(new KeyValuePair<string, string>("DeptId", "1C"));
secArray0.Add(new KeyValuePair<string, string>("Age", "25"));

And so on up-to secArray3. But how do I insert secArray0,secArray1,secArray2,secArray3 inside secArray? Just like the PHP code explained above.

Upvotes: 2

Views: 199

Answers (2)

Dmitry
Dmitry

Reputation: 14059

Use a Dictionary<string, string> for secArray0 and a Dictionary<string, string>[] for secArray:

var secArray0 = new Dictionary<string, string>
    {
        { "Name", "gomesh" },
        { "EmpCode", "ID04" },
        { "DeptId", "1C" },
        { "Age", "25" }
    };

...

Dictionary<string, string>[] secArray =
    {
        secArray0,
        secArray1,
        ...
    };

Upvotes: 2

Petar Ivanov
Petar Ivanov

Reputation: 93050

var secArray = new List<Dictionary<string, string>>();
var secArray0 = new Dictiionary<string, string>();

secArray0["Name"] = "Gomesh";
secArray.Add(secArray0);

Upvotes: 2

Related Questions