2
Reply

C# Writing dictionary to text file

David Smith

David Smith

Dec 13 2011 9:44 AM
11.4k

Is there another way in c sharp to to basically write a dictionary of values to a textfile without iterating or using a foreach.

at the moment i am using a foreach to iterate through the dictionary. Review below.


           StreamWriter sw = new StreamWriter(@"c:\ArrayInfo.csv");

           sw.WriteLine("Generated Coordinates:" + Environment.NewLine + Environment.NewLine);


            Dictionary<int, int> coordinatesDictionary = new Dictionary<int, int>();

            coordinatesDictionary.Clear();

            double[] arrayRangeInfo = Enumerable.Range(10, 12).Select(i => (i * .01)).ToArray();
        
            foreach (int i in Enumerable.Range(1, 100))
            {
               coordinatesDictionary.Add(i,i)l
            }

            foreach (var dictItem in coordinatesDictionary)
            {
                sw.WriteLine(String.Format("{0}, {1}", dictItem.Key, dictItem.Value));
            }

            sw.WriteLine(Environment.NewLine + Environment.NewLine);
            sw.WriteLine("Done!");
            sw.Close();

          

Answers (2)