2

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.

2
  • Use a Dictionary<string, string> for secArray0 and a Dictionary<string, string>[] for secArray. Commented Dec 26, 2015 at 12:14
  • Can you explain a bit elaborately how to do ....Dictionary<string, string>[] for secArray Commented Dec 26, 2015 at 12:24

2 Answers 2

2

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,
        ...
    };
Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.