0

I have a below dictionary.

  1. How to print dictionary values which is a list of strings as t1, t2 & t3?
  2. I could also use Dictionary<string, Tuple<string, string, string>>. Which one is better - tuple or dynamic?
Dictionary<string, dynamic> Dict = new Dictionary<string, dynamic>
            {
                {
                    "testKey",
                    new List<string> { "t1", "t2", "t3" }
                }
            };

foreach (KeyValuePair<string, dynamic> kvp in Dict)
{
    System.IO.File.AppendAllText("dictionary.txt", string.Format("{0} {1} {2}", kvp.Key, kvp.Value, Environment.NewLine));
}
vela
  • 147
  • 10
user584018
  • 10,186
  • 15
  • 74
  • 160
  • Possible duplicate of [Convert a list to a string in C#](https://stackoverflow.com/questions/4981390/convert-a-list-to-a-string-in-c-sharp) – mjwills Mar 13 '19 at 10:45
  • Tuples are serialized ugly, as their structure is design-time only. But this is the less expensive data representation available. You either need reflection to print tuple elements or if the structure is well known, juts format it directly. – ZorgoZ Mar 13 '19 at 10:48
  • 1
    Instead of the tuple or the dynamic object, it could be clearer if you use a class. – aloisdg Mar 13 '19 at 10:50
  • 3
    Why do you wish to use `dynamic` vs `List`? – mjwills Mar 13 '19 at 10:51
  • Every time you use `dynamic`, a kitten dies! Seriously, there's almost always a better way. – DavidG Mar 13 '19 at 10:55
  • 1
    Which is better? Probably neither. Use `List` if the number of items is variable, `ValueTuple` if not (`Dictionary`). Of course, making your own little class to hold data is always an option as well. – Jeroen Mostert Mar 13 '19 at 10:55
  • I recommend csv format for this type of requirements. – Ahmet Remzi EKMEKCI Mar 13 '19 at 10:57

0 Answers0