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);
}
}
}
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);
}
}
}