Jump to content
  • Advertisement
Sign in to follow this  
serratemplar

c#: a temporary inline array?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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);

Share this post


Link to post
Share on other sites
Thanks for the quick reply.

My first reaction to that was "well, using the new operator will allocate memory and there it will be, without a pointer to it, so I can't deallocate it"....but then, that's the C++ talking in me. C# has fancy garbage collection. =)

Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!