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

manuc66
  • 2,701
  • 29
  • 28

1 Answers1

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.

bolkay
  • 1,881
  • 9
  • 20
  • 1
    @CamiloTerevinto What else to make of it? The reference has it exactly implemented the way the documentation states `if values.Length == 0 || values[0] == null) return String.Empty;`. It's exactly what the OP asked – NotFound Jun 14 '19 at 08:20
  • 2
    This answer is fine :) – Stuart Jun 14 '19 at 08: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 '19 at 08: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 '19 at 08:27
  • 2
    "This question does not appear to be about programming " Never change StackOverflow. lol, absurd mod'ing. – Stuart Jun 14 '19 at 08:28
  • @Stuart That's the message that is shown when a question is closed with a custom message – Camilo Terevinto Jun 14 '19 at 08:29
  • @Flater You are thinking this question *should* be answered, it should not. A comment with a message to the documentation along with a VTC was good enough, just as we do with typos – Camilo Terevinto Jun 14 '19 at 08:30
  • 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 '19 at 08:32
  • @Flater I chose the wrong word, sorry for English not being my first language :) – Camilo Terevinto Jun 14 '19 at 08:33
  • 1
    I think this is an interesting question why they chose this behaviour. Lookat referencesource they explicitly check the first item is null, and if so return string.Empty then append the rest of the items in the array to the stringbuilder starting the loop at 1. It seems an odd decision choice! – Stuart Jun 14 '19 at 08:34
  • 2
    @Stuart: To be fair, "why did ...?" is _usually_ an open-ended question that mostly invites interpretations and opinions. That being said, an answer with justified claims (evidence, documentation, or an "word of god" answer by a .Net designer) would be interesting and valid, but it's going to also open the door to many unwanted opinion pieces. – Flater Jun 14 '19 at 08:35
  • 2
    Might be a good idea to move this conversation to a chat room. – Matthew Jun 14 '19 at 08:36
  • @Flater Re _"To be fair..."_: What you describe is _exactly_ happened the other day with [a "Why?.." question about Java](https://stackoverflow.com/q/56139760/2985643). I commented that the question was opinion based unless the poster was _"incredibly lucky and received an answer from the person who was responsible for deciding to deprecate finalize()"_, and lo and behold the person who was actually responsible gave a great answer to the question! The question also attracted a lousy answer, as you predicted. – skomisa Jun 15 '19 at 04:49