Untested C#:
static void WriteCsv<T>(T[,] data, string path)
{
char[] specialChars = {',','\"', '\n','\r'};
using (var file = File.CreateText(path))
{
int height = data.GetLength(0), width = data.GetLength(1);
for (int i = 0; i < height; i++)
{
if (i > 0) file.WriteLine();
for (int j = 0; j < width; j++)
{
string value = Convert.ToString(data[i, j]);
if (value.IndexOfAny(specialChars) >= 0)
{
value = "\"" + value.Replace("\"", "\"\"")
+ "\"";
}
if (j > 0) file.Write(',');
file.Write(value);
}
}
}
}
Which reflector translates as:
Private Shared Sub WriteCsv(Of T)(ByVal data As T(0 To .,0 To .), ByVal path As String)
Dim specialChars As Char() = New Char() { ","c, """"c, ChrW(10), ChrW(13) }
Using file As StreamWriter = File.CreateText(path)
Dim height As Integer = data.GetLength(0)
Dim width As Integer = data.GetLength(1)
Dim i As Integer
For i = 0 To height - 1
If (i > 0) Then
file.WriteLine
End If
Dim j As Integer
For j = 0 To width - 1
Dim value As String = Convert.ToString(data(i, j))
If (value.IndexOfAny(specialChars) >= 0) Then
value = ("""" & value.Replace("""", """""") & """")
End If
If (j > 0) Then
file.Write(","c)
End If
file.Write(value)
Next j
Next i
End Using
End Sub