Click to See Complete Forum and Search --> : Dictionary questions


prixone
January 16th, 2010, 04:44 PM
Hi,

this is just a sample gathered from elsewhere where i would like to go for a few doubts.

To begin with you need 2 args on the Dictionary, key and value, but even so does it still store the data Enumerated (like "key cat, value 2" is stored on position 0) ?

What would be the best approch to Enumerate my data ?
Some way to reuse empty positions, for example, i have 10 items on it, but item 5 is null so the best way would be going thru all data to actually know that 5 is null (considering ofc u dont know it) and fill it up with some other data ? or is there an easier way of doing it (like just using d.Add(data,data) it would take in place on the null item, for example) ?


using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// Example Dictionary again
Dictionary<string, int> d = new Dictionary<string, int>()
{
{"cat", 2},
{"dog", 1},
{"llama", 0},
{"iguana", -1}
};
// Loop over pairs with foreach
foreach (KeyValuePair<string, int> pair in d)
{
Console.WriteLine("{0}, {1}",
pair.Key,
pair.Value);
}
// Use var keyword to enumerate dictionary
foreach (var pair in d)
{
Console.WriteLine("{0}, {1}",
pair.Key,
pair.Value);
}
}
}

nelo
January 16th, 2010, 05:22 PM
To begin with you need 2 args on the Dictionary, key and value, but even so does it still store the data Enumerated (like "key cat, value 2" is stored on position 0) ?

I'm not sure about that. All I would say on that point is that is a dictionary not an array where you can index by position.

What would be the best approch to Enumerate my data ?

The sample you have shows you the best way to enumerate through the data.

Some way to reuse empty positions, for example, i have 10 items on it, but item 5 is null so the best way would be going thru all data to actually know that 5 is null (considering ofc u dont know it) and fill it up with some other data ? or is there an easier way of doing it (like just using d.Add(data,data) it would take in place on the null item, for example) ?
There isn't any null item as such. You have key value pairs. You may choose to put a null value against a particular key. You would have to iterate through the dictionary to see which keys have null values stored against. I would also mention at this point that not every dictionary will allow null values (e.g. Dictionary<String, Int32>). Think of it as a dictionary a collection of key,value pairs.

private void Test()
{
var test = new Dictionary<String, Int32>();

// The line below will set the value 35 against the key A. If the key doesn't exist it will
// be created and initialised with the value 35.
test["A"] = 35;

var test2 = new Dictionary<String, String>();

// You can also use the Add method to add entries into the dictionary. Each entry is a
// key value pair. You cannot have a null value unless you actually specify (if the definition
// of the dictionary permits it)
test2.Add("A", null);
}


Hope this helps.

Mutant_Fruit
January 17th, 2010, 07:24 AM
To begin with you need 2 args on the Dictionary, key and value, but even so does it still store the data Enumerated (like "key cat, value 2" is stored on position 0) ?

Nope. The documentation explicitly states that the keys are not in any specific order.


What would be the best approch to Enumerate my data ?

If you need to enumerate the keys in the same order you add them to your container, use a List<> or something similar.

prixone
January 18th, 2010, 10:16 PM
Thanks for the replies.

Nelo yes that is what i currently do, my own keys etc i was just trying to find out this information to make sure that was the way to go.