Reputation: 418
I need some help. I have a method that should output a txt file with the contents of a list(each item on a line). The list items are string arrays. The problem is that when I call string.Join
it returns the literal string "System.String[]"
instead of the actual concatenated string. I watched it in the debugger and the list and its arrays have the correct strings. Any idea how I can get the actual string so it can be written in the txt file?
Here is my code :
public void myMethod()
{
List<Array> list = new List<Array>();
for (int i = 0; i < myGrid.Rows.Count; i++)
{
string[] linie = new string[3];
linie[0] = myGrid.Rows[i].Cells[1].Value.ToString();
linie[1] = myGrid.Rows[i].Cells[3].Value.ToString();
linie[2] = myGrid.Rows[i].Cells[2].Value.ToString();
{
list.Add(linie);
}
}
string path = "S:\\file.txt";
StreamWriter file = new System.IO.StreamWriter(path);
foreach (Array x in list)
{
string s = string.Join(",", x);
file.WriteLine(s);
}
file.Close();
}
Upvotes: 6
Views: 4511
Reputation: 152566
Here's the reason you're getting those results:
The overloads for string.Join are:
Join(string, params object[])
Join(string, params string[])
Join(string, params string[], int, int)
Join(string, IEnumerable<string>)
Join<T>(string, IEnumerable<string>)
Since your second parameter to string.Join
is an untyped Array
, which is neither a string[]
nor an IEnumerable<T>
, the "best" overload is Join(string, params object[])
, with the Array
treated an an object[]
with a single value - the Array
(not an object[]
with several string
or object
values). So Join calls ToString()
on the Array
, which returns System.String[]
since that is the actula type of the Array.
There are multiple ways to fix it, but to me the cleanest is to use List<string[]>
instead of List<Array>
since you only ever store strings in the array. That way the overload 'Join(string, string[])` will be called which gives you the results you expect:
public void myMethod()
{
List<string[]> list = new List<string[]>();
for (int i = 0; i < myGrid.Rows.Count; i++)
{
string[] linie = new string[3];
linie[0] = myGrid.Rows[i].Cells[1].Value.ToString();
linie[1] = myGrid.Rows[i].Cells[3].Value.ToString();
linie[2] = myGrid.Rows[i].Cells[2].Value.ToString();
{
list.Add(linie);
}
}
string path = "S:\\file.txt";
StreamWriter file = new System.IO.StreamWriter(path);
foreach (string[] x in list)
{
string s = string.Join(",", x);
file.WriteLine(s);
}
file.Close();
}
Upvotes: 2
Reputation: 21245
Change the type of
List<Array> list = new List<Array>();
to
List<string[]> list = new List<string[]>();
You can use File.WriteAllLines
.
string[] contents = list.Select(a => string.Join(",", a)).ToArray();
File.WriteAllLines(path, contents);
Upvotes: 3
Reputation: 887489
string.Join()
takes a params object[]
.
When you pass a variable of compile-time type Array
, it interprets that as a single item in the array, and it ends up passing new object[] { yourArray }
.
It then calls ToString()
on the single item, which returns the type name (since arrays do not override ToString()
.
To fix that, you need to change Array
to any strongly-typed collection type (string[]
, object[]
, or IEnumerable<string>
).
It will then pass the array itself to Join()
, instead of assuming it's a single parameter in the paramarray.
Upvotes: 19