Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do you offload a Dictionary?

How do you offload a Dictionary?

Scheduled Pinned Locked Moved C#
questioncsharp
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xarzu
    wrote on last edited by
    #1

    In C#, how do you output the contents of a Dictionary class? Once you have loaded a Dictionary class with keys and values, how do I cycle through them and output the individual values in a foreach loop?

    L N OriginalGriffO A 4 Replies Last reply
    0
    • X Xarzu

      In C#, how do you output the contents of a Dictionary class? Once you have loaded a Dictionary class with keys and values, how do I cycle through them and output the individual values in a foreach loop?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      foreach (KeyValuePair<type1, type2> pair in dict)
      {
      Console.WriteLine("{0}, {1}",
      pair.Key,
      pair.Value);
      }

      Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

      1 Reply Last reply
      0
      • X Xarzu

        In C#, how do you output the contents of a Dictionary class? Once you have loaded a Dictionary class with keys and values, how do I cycle through them and output the individual values in a foreach loop?

        N Offline
        N Offline
        Niladri_Biswas
        wrote on last edited by
        #3

        Try this

        Dictionary<object, object> dummyDictionary = new Dictionary<object, object>
        {
        {"India","Delhi"},
        {"USA","WashingtonDC"},
        {"Bangaladesh","Dhaka"},
        {"Pakistan","Karachi"}
        };

        //Foreach loop construct

        foreach (KeyValuePair<object, object> kvp in dummyDictionary)
        {
        Console.WriteLine(string.Format("Key = {0} Value = {1}", kvp.Key, kvp.Value));
        }

        //using Linq and Foreach Extension method

        var result =
        (from kvp in dummyDictionary
        select new
        {
        Key = kvp.Key
        ,
        Value = kvp.Value
        });
        result.ToList().ForEach(kvp => Console.WriteLine(string.Format("Key = {0} Value = {1}", kvp.Key, kvp.Value)));

        Console.ReadKey();

        Niladri Biswas (Code Project MVP 2012)

        1 Reply Last reply
        0
        • X Xarzu

          In C#, how do you output the contents of a Dictionary class? Once you have loaded a Dictionary class with keys and values, how do I cycle through them and output the individual values in a foreach loop?

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          There is another way as well, which can be more readable. Assuming Track is one of your classes:

          Dictionary<string, Track> dict = new Dictionary<string, Track>();
          foreach (Track track in DALFactory.GetAll<Track>())
          {
          dict.Add(track.TrackName, track);
          }
          ...
          foreach (string key in dict.Keys)
          {
          if (key.StartsWith("A"))
          {
          Track track = dict[key];
          Console.WriteLine(track);
          }
          }

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • X Xarzu

            In C#, how do you output the contents of a Dictionary class? Once you have loaded a Dictionary class with keys and values, how do I cycle through them and output the individual values in a foreach loop?

            A Offline
            A Offline
            Abhinav S
            wrote on last edited by
            #5

            A number of approaches are described here[^].

            A new Windows Phone App - Speed Dial

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups