Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this an undocumented override of the Split method?

Tags:

c#

I just found that not only does this code compile, it seems to split the string on any whitespace.

List<string> TableNames = Tables.Split().ToList();

However it didn't show in the intellisense and it's not on the MSDN page.

Is this just an undocumented override? And is it dangerous to use because of that?

like image 664
William Mioch Avatar asked Jun 23 '11 03:06

William Mioch


1 Answers

It's not an override. In this case, the compiler translates Split() into Split(char[]) with an empty parameter.

Split is defined as

public string[] Split(
    params char[] separator
)

params lets you specify a variable number of arguments, including no arguments at all. When no arguments are provided (as is in your example), the separator array will be empty.

From the MSDN page linked above:

If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters.

This is why you're seeing the string split on whitespace. This is just default behaviour rather than an undocumented feature, so you're free to use it without fear of unusual side-effects. Well, unless default behaviour changes in a future version of .NET, but that seems fairly unlikely to me since whitespace is a reasonable default.

like image 187
Adam Lear Avatar answered Oct 13 '22 00:10

Adam Lear