• 12
• 12
• 9
• 10
• 13

c#: a temporary inline array?

This topic is 3565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I want to print multiple formatted columns to a text file, using only two String.Format calls: 1. The first is for the column headers, which are static. 2. The last is nested within a loop to print each line below the headers. So for instance
A     B     C
1     4     7
2     5     8
3     6     9

My question is, how do I format the call to String.Format to do this? For instance: String.Format("{0,-5}{1,-5}{2,-5}", {"A","B","C"}); That won't work (as I've seen); I'm trying to declare a String array inline (at the end there) which, while intuitive, isn't in the language. Is there a way to do this? Should I just bite the bullet and create the String array on a preceding line? (Would that end up being prettier anyway?) This question is purely academic, but I am curious. If anyone knows the answer, I would appreciate them sharing. Thanks for your time. EDIT: When I said "ArrayList" I was thinking "String array", as in String[] s = {"A", "B", etc.} EDIT: Slight tweak so you could see my silly formatting.

Share on other sites
It is possible to create an array "inline". For example, a call to String.Split() can look like this:

s.Split(new char[] { '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries);