No it doesn’t…  Yes it does…

Working on a pre-existing application the other day, I came across what I think is a strange behavior with the String.Split() method.  The application has a very complex permissions structure and many of the permission sets start out as delimited strings.  The strings eventually get split into arrays to do the appropriate actions based on the permissions. 

All was fine until we started adding new functionality.  The strange behavior showed up as a null object exception.  There was a method that was expecting a permission string, but was instead getting an empty string.  I didn’t know this, but splitting on an empty string returns an array with one element, containing an empty string.  For example:

string test = "";
string [] testArrray = test.Split( ',' );

Console.WriteLine(testArray.Length.ToString());
Console.WriteLine(testArray[0].ToString());

The first WriteLine would output 1; the second line would output an empty string.  I thought about this, and to me it just doesn’t seem right.  I thought about it some more, and it maybe did seem right.  I finally settled on it doesn’t seem right.  The code that was doing this in the application never knew what would be coming in the permission string.  In some instances, the string could be empty for a new user who didn’t have permissions in a certain group.  I think that it makes more sense to return an empty array, but maybe I’m applying it to much to this one situation.  However, I can’t help but think “It’s an empty string with no delimiters, why am I getting back an array with more than no elements?”

Anyone else think this is weird?  Or am I?