CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    34

    Dictionary questions

    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) ?


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

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Dictionary questions

    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.
    Code:
    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.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Dictionary questions

    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Dec 2009
    Posts
    34

    Re: Dictionary questions

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured