-1

I am wanting to output the whole array in a single line. I know how to do one each line which would be:

Array.ForEach(array1, Console.WriteLine);

I saw that to print it all out in 1 line it is like this:

Console.WriteLine("[{0}]", string.Join(", ", array1));

But i don't really understand what that piece of code is actually doing - If there is a more basic version to understand please let me know, if not then could you please explain what it is doing?

Dan
  • 137
  • 2
  • 9
  • Possible duplicate of [printing all contents of array in C#](https://stackoverflow.com/questions/16265247/printing-all-contents-of-array-in-c-sharp) – Anas Alweish Sep 25 '19 at 19:46
  • https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=netframework-4.8 – Corentin Pane Sep 25 '19 at 19:47
  • `string.Join` accepts a string that gets inserting between each element of the array, and an array to join together. So, your second code is saying: Join the elements of `array1` with `", "` (a comma and a space) in between each element. Does this clear it up a little? Where are you confused? – dvo Sep 25 '19 at 19:48
  • What is the purpose of ```"[{0}]"```? @dvo – Dan Sep 25 '19 at 19:50
  • @XxDanxX That is the same as [string.Format](https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8#System_String_Format_System_String_System_Object_) where the `{0}` is replaced by the first object passed after the comma. So this will print out: `[{your array}]` (replacing the `{0}` with your array string). This will print the same as `"[" + {your string.Join} + "]"`. – dvo Sep 25 '19 at 19:53
  • @dvo Thanks, that slightly clears up the confusion. But I'm still kinda confused on the whole string.Join - Should I just remember that the whole point of it, is tolo go in array and join all the elements together? - Also, you said string.Join accepts a string. But how does it accept a string when my array is numbers, not characters? Lastly, with the {0} part how does it know to place that in between the brackets? It is after in speech marks so why does it input the element instead of 0? – Dan Sep 25 '19 at 20:03
  • @XxDanxX The array can be an array of anything. It will print whatever would print if you called `.ToString()` on each element of the array. As for your other question. Exactly "`{0}`" gets replaced. So if you had `"foo{0}bar", "ANY"`, it would output `fooANYbar`. Hope that makes sense. – dvo Sep 25 '19 at 20:07
  • It kinda makes sense. However I'm still confused lol - It's all good, I mainly understand it. Thanks for clarifying it up a bit for me! @dvo – Dan Sep 25 '19 at 20:12

1 Answers1

2

It might help to understand that this code:

Console.WriteLine("{0}", string.Join(", ", array1));

Is pretty much the same as this code:

string allContents = string.Join(", ", array1);
Console.WriteLine(allContents);

Microsoft Docs defines string.Join as follows:

Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.

If you ever don't understand what a method does, you can look it up there for some help :)

The "{0}" part of the string is replaced by the first (index 0) argument which comes after it. So in your case, this is the evaluation of "string.Join(", ", array1)". This is known as Composite Formatting, and you can read more about it here.

Basically, when Console.WriteLine encounters numbers (or special formats) enclosed in curly brackets, along with additional objects (which specify the objects to be formatted), then it treats those curly brackets as special characters and it formats the values passed into them accordingly. If you want to actually print the curly bracket characters, then you need to 'escape' them by using "{{" instead of "{", and "}}" instead of "}".This can be demonstrated by testing some variations:

Console.WriteLine("{0} {1} {2} {3} {4}", "this", "is", "a", "test", "string"); // Prints "this is a test string".
Console.WriteLine("{0} {1} {0} {4} {3}", "this", "is", "a", "test", "string"); // Prints "this is this string test" - note you can order the arguments whatever way you like and also reuse them.
Console.WriteLine("{{0}}", string.Join(", ", array1)); // Prints "{0}", as the curly brackets are 'escaped', meaning they are treated literally. The other arguments are ignored as no string contained within curly brackets was detected.
Console.WriteLine("{0}"); // Prints "{0}" exactly as it's written, as there are no arguments to be formatted.
Console.WriteLine("[{asdf}]", string.Join(", ", array1)); // throws an exception, because an invalid format is enclosed in curly brackets.
Liam Tuite
  • 21
  • 4