0

I have an array or a list from linq. I want to show it as a string in console! What should I do?

6 Answers 6

7
String.Join(delimiter, array);

You could represent it as:

Console.WriteLine("{" + String.Join(", ", array) + "}");

Of course, I think this only works with strings.

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

Comments

2

The most generic answer that I can give to you is to loop through each element and use the ToString() method on each element.

Alternatively, you can serialize the Array/List to Xml.

1 Comment

Thanks, I decided to use Json serializer, its visually simple
1

Just iterate over it?

foreach (var item in list)
{
   Console.WriteLine(item.ToString());
}

Comments

0

Generally you can loop through it if it's a collection or an array. Check the foreach keyword

List<Object> list = ...

foreach (Object o in list) {
  Console.WriteLine(o.ToString);
}

Comments

0

If you'd like a more LINQ approach you could use the following:

String text = String.Join("," + Environment.NewLine, list.Select(item => item.ToString()).ToArray());
Console.WriteLine(text);

The first parameter of the Join specifies which characters should be inserted between items in the array. Using the .Select on the list is for getting a string representation of your item in the array.

Comments

-1

I would want some more information about exactly what you want to see, but at first blush I'd try something like:

public string StringFromArray(string[] myArray)
    {
        string arrayString = "";
        foreach (string s in myArray)
        {
            arrayString += s + ", ";
        }
        return arrayString;
    }

2 Comments

You've just reinvented String.Join
@Bart, Not quite: this version will add a pointless extra ", " at the end of the joined string. It'll also be slower and eat more memory than string.Join ;)

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.