none
print dictionary contain list c#

    Question

  • Hello :)

    how can i print a dictionary contain a list c#

    Dictionary

    <string, List<string>> FullInfo = newDictionary<string, List<string>>();

    thank you:)

    Thursday, April 13, 2017 9:45 AM

Answers

  • Hope following code can help you

     Dictionary<string, List<string>> FullInfo = new Dictionary<string, List<string>>();
                FullInfo.Add("Cities", new List<string> { "Paris", "Chicago", "Tokyo", "Seoul" });
                FullInfo.Add("Countries", new List<string> { "France", "US", "Japan", "Korea" });
                FullInfo.Add("Number", new List<string> { "One", "Two", "Three", "Four" });
    
                foreach( var contents in FullInfo.Keys)
                {
                    
                    foreach(var listMember in FullInfo[contents])
                    {
                        Console.WriteLine("Key : " + contents + " member :" + listMember);
                    }
                }

    The response was 

    Key : Cities member :Paris
    Key : Cities member :Chicago
    Key : Cities member :Tokyo
    Key : Cities member :Seoul
    Key : Countries member :France
    Key : Countries member :US
    Key : Countries member :Japan
    Key : Countries member :Korea
    Key : Number member :One
    Key : Number member :Two
    Key : Number member :Three
    Key : Number member :Four


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora


    • Edited by RohitArora Thursday, April 13, 2017 11:57 AM
    • Proposed as answer by Ehsan Sajjad Thursday, April 13, 2017 12:56 PM
    • Marked as answer by kauthar1992 Monday, April 24, 2017 9:46 AM
    Thursday, April 13, 2017 11:50 AM

All replies

  • Hi kauthar1992,

    You have to access the item with "Key" and "Value"

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


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Thursday, April 13, 2017 11:17 AM
    Moderator
  • Hope following code can help you

     Dictionary<string, List<string>> FullInfo = new Dictionary<string, List<string>>();
                FullInfo.Add("Cities", new List<string> { "Paris", "Chicago", "Tokyo", "Seoul" });
                FullInfo.Add("Countries", new List<string> { "France", "US", "Japan", "Korea" });
                FullInfo.Add("Number", new List<string> { "One", "Two", "Three", "Four" });
    
                foreach( var contents in FullInfo.Keys)
                {
                    
                    foreach(var listMember in FullInfo[contents])
                    {
                        Console.WriteLine("Key : " + contents + " member :" + listMember);
                    }
                }

    The response was 

    Key : Cities member :Paris
    Key : Cities member :Chicago
    Key : Cities member :Tokyo
    Key : Cities member :Seoul
    Key : Countries member :France
    Key : Countries member :US
    Key : Countries member :Japan
    Key : Countries member :Korea
    Key : Number member :One
    Key : Number member :Two
    Key : Number member :Three
    Key : Number member :Four


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora


    • Edited by RohitArora Thursday, April 13, 2017 11:57 AM
    • Proposed as answer by Ehsan Sajjad Thursday, April 13, 2017 12:56 PM
    • Marked as answer by kauthar1992 Monday, April 24, 2017 9:46 AM
    Thursday, April 13, 2017 11:50 AM
  • thank you :)
    Monday, April 24, 2017 9:47 AM