Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "my,string".Split(',') works in .NET C#

Tags:

c#

.net

split

Why "my,string".Split(',') works in .NET C# ?

The declaration of Split according to MSDN is Split(Char[]). MSDN String.Split Method

I supposed that C# 5 converts the single char ',' to char[] {','}; But I must be wrong because the following code doesn't work:

static void Main()
{
    GetChar(',');
}

static char GetChar(char[] input)
{
    return input[0];
}

EDIT: Thanks to the Jon Skeet's answer I changed the argument to params char[] and it works proving the concept.

static char GetChar(params char[] input)
{
    return input[0];
}
like image 449
Miroslav Popov Avatar asked Jul 29 '13 20:07

Miroslav Popov


People also ask

How can I split a string in C#?

Split(char[]) Method This method is used to splits a string into substrings that are based on the characters in an array. Syntax: public String[] Split(char[] separator); Here, separator is a character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.

Can you use split on a string?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How do I split a string into a new line?

Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .

Why we use split in C#?

The Split() method is part of the string class in C#. The method is used to split a string based on the delimiters passed to the string. The delimiters can be a character, an array of characters, or even an array of strings. We can also pass the function an array of strings to be split on the delimiters passed to it.


1 Answers

The overload you're using uses a parameter array, basically. That's what the params part is. The compiler automatically wraps up your single argument into an array. So this:

var x = text.Split(',');

is equivalent to:

var x = text.Split(new char[] { ',' });

You can use a parameter array for your own methods too, with the params keyword:

static char GetChar(params char[] input)
{
    return input[0];
}

Note that the parameter array has to be the final parameter. That is why the overload you're using is the only overload of Split to use a parameter array. Look at the other overloads:

Split(Char[], Int32)
Split(Char[], StringSplitOptions)
Split(String[], StringSplitOptions)
Split(Char[], Int32, StringSplitOptions)
Split(String[], Int32, StringSplitOptions)

In each of these cases, the array is the first parameter, so you have to construct an array yourself:

var x = text.Split(new char[] { ',' }, 10); // Call the (char[], int) overload

Or using an implicitly-typed array:

var x = text.Split(new[] { ',' }, 10); // Call the (char[], int) overload
like image 69
Jon Skeet Avatar answered Sep 19 '22 15:09

Jon Skeet