6

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();
  }      
5
  • Have you tried string.Concat(",", x)? Commented May 7, 2013 at 13:34
  • @gwin003: That won't help. Commented May 7, 2013 at 13:34
  • "SystemString[] " is the actual string that it returns. Commented May 7, 2013 at 13:34
  • 1
    Why are you defining list as List<Array> instead of List<string[]>? I'm guessing that it is getting confused and using the LINQ join method (which joins generic arrays together into another array) instead of the string.join method, which joints string arrays together into a single string. Commented May 7, 2013 at 13:35
  • change all uses of Array to string[] and it should work fine. Commented May 7, 2013 at 13:36

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Kudos on actually understanding the real question. Formatting in the question could've saved that hehe. +1 from me.
@Niels: I completely misunderstood the problem. See my edited answer.
Lol, confusion all around. I'll edit the question a bit to clarify.
The general lesson is to avoid the use of Array.
Thank you SLaks and others for the answers. I'll mark this as the answer. And thank you Niels for clarifying the question.
3

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);

Comments

2

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();
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.