dotnet / dotnet/dotnet-api-docs

string.Join with only null values returns delimiters instead of string.Empty

Open
#5,116 6 comments 0 reactions 0 assignees View on GitHub
area-System.Runtime Pri3 untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

### Description

According to `string.Join(char, params string[])` method description:
> Returns:
A string that consists of the elements of value delimited by the separator character. -or- `string.Empty` if value has zero elements or all the elements of value are null.

It should return `string.Empty` when all `values` are `null`. Well it does not, it returns string containing only the separator(s).

```cs
var values = new string[] { null, null, null };
var result1 = string.Join(';', values); // result1 = ";;"
var result2 = string.Join(';', null, null, null); // result2 = ";;"

var result3 = string.Join(";", values); // result3 = ";;"
var result4 = string.Join(";", null, null, null); // result4 = ";;"
```

It works the same for `string.Join(string, params string[])` although its description does not mention case when `all the elements of value are null`:
> Returns:
A string that consists of the elements in value delimited by the separator string. If value if an empty array, the method returns `string.Empty`.

Descriptions on [MSDN](https://docs.microsoft.com/en-us/dotnet/api/system.string.join?view=netcore-3.1) are also inconsistent, where only `Join(String, Object[])` and `Join(String, String[])` do not have `all the elements of values are null` mentioned.

I do not know how this should be handled, probably adjusting descriptions to match current behaviour is the most reasonable way to handle this but maybe it worked at some point and behaviour should be fixed.

### Configuration

First found on .NET Core 3.1.
Windows 10 Professional x64

### Regression?

`string.Join(char, params string[])`
Tested with .NET Core 2.0, 3.0, 3.1

`string.Join(string, params string[])`
Tested with .NET Core 2.0, 3.0, 3.1 and .NET Framework 4.6, 4.7.2.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.