Here is one of the options that I like. Itβs better if you already have an IEnumerable<string> with your data, but it's easy enough even if you donβt. It also scales well to n lines that are concatenated, not just 1 or two.
string[] myStrings = new string[]{"Hello", "World", null}; string result = string.Join(" ", myStrings.Where(str => !string.IsNullOrEmpty(str)));
Here is another option. This is a little shorter for this one case, but it is uglier, harder to read, and not as extensible, so I will probably personally avoid it:
//note space added before {0} Assert.Fail("Something is foo. {0}", message ?? "\b");
In this case, we will add a space to the format string, but if message is null, we use the backspace character instead to remove the space that we know before it in the message.
Servy
source share