C# Dictionary to .csv

c#, csv, file

Solution

Maybe the easiest:

String csv = String.Join(
    Environment.NewLine,
    data.Select(d => $"{d.Key};{d.Value};")
);
System.IO.File.WriteAllText(pathToCsv, csv);

You'll need to add `using LINQ` and use at least .NET 3.5

Problem

I have C# `Dictionary` and I want create a `.csv` file from it. For example I have this dictionary: ``` Dictionary<string, string> data = new Dictionary<string, string>(); data.Add("0", "0.15646E3"); data.Add("1", "0.45655E2"); data.Add("2", "0.46466E1"); data.Add("3", "0.45615E0"); ``` And I wanna this `.csv` file output: ``` 0;0.15646E3; 1;0.45655E2; 2;0.46466E1; 3;0.45615E0; ``` How can I do this?

Original source