Behaviour of string.Join

Viewed 3928

I'm using string.Join() to concatenate values with delimiters - here is my simplified example using User class:

public class User
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Here are my results:

User uItem = new User() { Age = 10, Name = null };

string item1 = string.Join(";", string.Empty, 1); // returns ";1"  
string item2 = string.Join(";", null, 1); // returns ""        
string item3 = string.Join(";", uItem.Name, uItem.Age, 1); // returns ""
string item4 = string.Join(";", 1, null); //returns "1;"
string item5 = string.Join(";", null, uItem.Age, 1); // throws null exception

First I was surprised that item2 doesn't behave like item1 and determines a "" instead of ";1".

Is there a rule like null is allowed but not at the first item?

My question: why does item3 return "" and item5 throws a exception? The input values are equal.

2 Answers
Related