user1678541
user1678541

Reputation:

"Travel" through Array

This is how my array should look:

key : value

name : victor
age : 16
country : romania
city : bucharest
language : romania

How can I pass trough all it's elements to obtain something like:

Console.WriteLine("{0} = {1}", /*keyName*/, /*keyValue*/);

Upvotes: 0

Views: 466

Answers (2)

codingbiz
codingbiz

Reputation: 26376

It should be

Dictionary<string, object> dics = new Dictionary<string, object>();

dics.Add("name","victor");
dics.Add("age",16);
dics.Add("country","romania");

foreach(var key in dics.Keys)
{
   Console.WriteLine("{0} = {1}", key, dics[key]);
}

Upvotes: 1

Euphoric
Euphoric

Reputation: 12849

That is not an array. That is dictionary.

To iterate over it, use either for loop or foreach loop.

example:

        var dictValues = new Dictionary<string, string>();
        // fill dictValues
        foreach(var items in dictValues)
            Console.WriteLine("{0} = {1}", items.Key, items.Value);

Upvotes: 4

Related Questions