方法参数中的 char[] 与 char。这是 MS 文档中的错误吗?

char[] vs char in argument of method. Is this a Mistake in MS documentation?

根据微软文档:

system.string 命名空间中的方法 Split 将 char 数组作为参数。

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

我知道我可以这样使用它:

string[] myarray1=null;    
string somestring="Hi, My name is Tamara";
myarray1=somestring.Split(','); // i used char, not char[] and everything works fine...

我的问题是为什么这个方法工作正常?我使用的是字符,而不是字符数组。 我无法理解这一点。也许这个问题很愚蠢,但试图提高我对文档的理解;/

注意关键字 params。这意味着,您可以在不创建字符数组的情况下调用该方法:.Split('a', 'b', 'c')。但是,如果你已经有一个 char 数组,你也可以这样调用方法:

char[] chars = new char[] {'a', 'b', 'c'};
string somestring="Hi, My name is Tamara";
var s = somestring.Split(chars);

第一种方式只是语法糖。

来自 Docs

You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type. You also can send no arguments. If you send no arguments, the length of the params list is zero.