Is this an undocumented override of the Split method?

I just found out that compilation not only compiles, but seems to break the string into any spaces.

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

However, it did not appear in intellisense, and not on the MSDN page.

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

+6
source share
2 answers

This is not an override. In this case, the compiler translates Split() to Split(char[]) with an empty parameter.

Split is defined as

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

params allows you to specify a variable number of arguments, including no arguments at all. If there are no arguments (as in your example), the separator array will be empty.

On the MSDN page linked above:

If the delimiter parameter is zero or does not contain characters, it is assumed that the space characters are delimiters.

This is why you see line splitting into spaces. This is the default behavior, not an undocumented feature, so you can use it without fear of unusual side effects. Well, if the default behavior does not change in a future version of .NET, but this seems unlikely to me, since spaces are a reasonable default.

+10
source

From the note to String.Split :

If the delimiter parameter is nothing or does not contain characters, spaces characters are considered delimiters. White space characters defined by the Unicode standard and return true if they are passed to the Char.IsWhiteSpace method.

I call this documented behavior personally. :)

+3
source

Source: https://habr.com/ru/post/891177/


All Articles