If string is empty or null,
Shouldn’t string.split(“;”) should throw an error ?
for me I am trying this code and goes through it without any error,
string a = string.empty;
if (a.Split(';').Length - 1 < 1)
Can anyone tell me why it not throws an error and why if statement is true.
If the string is null,
.Split()will (obviously) throw aNullReferenceException, like any other instance method.If the string is empty,
.Split()will return an array of a single empty string (unless you passStringSplitOptions.RemoveEmptyEntries).This is a corner case of its more general (and less unexpected) behavior; if the delimiter does not appear anywhere in the source string, it will return an array containing the entire source string.