2

I noticed that string.Join(" ", new object[] { null, "a", null, "b"}) returns string.Empty

Notice that string.Join(" ", new string[] { null, "a", null, "b"}) returns a b (as I would expect)

Things get vicious when concatenating characters: string.Join(" ", new object[] { null, 'a', null, 'b'}) which also returns string.Empty

Edit: Even though the documentation says it (thanks to @elgonzo), is there a valid reason such exception was implemented in the framework ?

See https://dotnetfiddle.net/uM9SVp

3
  • Hack Console.WriteLine(string.Join(",",(new object[] { null, "a", null, "b"}).Select(s=>s)));
    – TheGeneral
    Jun 14, 2019 at 8:09
  • 5
    I'm voting to close this question as off-topic because the behaviour seen is the expected one, as the documentation states. Jun 14, 2019 at 8:11
  • 1
    @CamiloTerevinto Do you close other million questions which have answers in documentation as well? Jun 14, 2019 at 8:32

1 Answer 1

6

As per the docs:

Notes to Callers

If the first element of values is null, the Join(String, Object[]) method does not concatenate the elements in values but instead returns Empty. A number of workarounds for this issue are available. The easiest is to assign a value of Empty to the first element of the array, as the following example shows.

object[] values = { null, "Cobb", 4189, 11434, .366 };
if (values[0] == null) values[0] = String.Empty;
Console.WriteLine(String.Join("|", values));
// The example displays the following output:
//      |Cobb|4189|11434|0.366

I suggest you read it up, pretty interesting.

13
  • 2
    This answer is fine :)
    – Stuart
    Jun 14, 2019 at 8:21
  • 3
    @CamiloTerevinto The post literally starts with a link to the documentation. That's not plagiarism. That's saving people time to show exactly the information they are after without needing to scan a page that might in the future at some point even disappear rendering the awnser useless
    – NotFound
    Jun 14, 2019 at 8:26
  • 5
    @CamiloTerevinto: Question closure aside, it's massively inappropriate to label this as plagiarism. Bolkay linked to the source and did not pretend like they are the author. If anything, SO (and SE) specifically disallows link-only answers and expects the relevant information to be copied into the answer because links might break.
    – Flater
    Jun 14, 2019 at 8:27
  • 2
    "This question does not appear to be about programming " Never change StackOverflow. lol, absurd mod'ing.
    – Stuart
    Jun 14, 2019 at 8:28
  • 3
    @CamiloTerevinto: You're dodging the topic. I specifically started off with "Question closure aside" and replied to your accusation of plagiarism. If question closure is warranted (which I find debatable but am willing to give you the benefit of the doubt there), that is no reason to wantonly accuse people of plagiarism. If anything, bolkay complied with the SO requirement to avoid link-only answers.
    – Flater
    Jun 14, 2019 at 8:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.