The code splits the string into components separated with ';' - the result of this operation is the array of strings. If there are less than 2 components the condition is true.
If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance.
An empty string clear does not contain any of the characters in separator, hence an array is returned consisting of a single element referring to an empty string.
Of course, if you call Split on a null reference, you'll get a NullReferenceException. It's important to differentiate between a reference to an empty string and a null reference.
If you want the method to return an empty array, use StringSplitOptions.RemoveEmptyEntries. If you want the result to be an error, you should check for this yourself and throw whatever exception you want.
It's important not to guess at behaviour when using an API though: if you're in any doubt at all, consult the documentation.
If the string is null, .Split() will (obviously) throw a NullReferenceException, like any other instance method.
If the string is empty, .Split() will return an array of a single empty string (unless you pass StringSplitOptions.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.
An empty string is NOT the same as a null string. Strings, being reference types will always contain "" if empty. Null is not at all the same thing, thus, if you have an empty string, it will have a length of 0 and your if statement will be valid.
From your code, a isn't null, it's String.Empty. So when you split an empty length string by a semicolon, there's 1 item. 1 - 1 is less than 1
The code splits the string into components separated with
';'
- the result of this operation is the array of strings. If there are less than 2 components the condition is true.It should behave as documented:
An empty string clear does not contain any of the characters in
separator
, hence an array is returned consisting of a single element referring to an empty string.Of course, if you call
Split
on a null reference, you'll get aNullReferenceException
. It's important to differentiate between a reference to an empty string and a null reference.If you want the method to return an empty array, use
StringSplitOptions.RemoveEmptyEntries
. If you want the result to be an error, you should check for this yourself and throw whatever exception you want.It's important not to guess at behaviour when using an API though: if you're in any doubt at all, consult the documentation.
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.
An empty string is NOT the same as a null string. Strings, being reference types will always contain "" if empty. Null is not at all the same thing, thus, if you have an empty string, it will have a length of 0 and your if statement will be valid.