为什么一些字符串函数用“params”声明,其他字符串不是? [英] Why are some string functions declared with "params", others not?

查看:73
本文介绍了为什么一些字符串函数用“params”声明,其他字符串不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没问题,只是一个问题:



您可以写:trimmed = untrimmed.Trim('(',')','[',' ]');

但你不能写:pos = line.IndexOfAny('(',')','[',']');



...'trimChars'是'params char []','anyOf'是'char []'(没有'params')。你必须写:pos = line.IndexOfAny(new char [] {'(',')','[',']')};



是有没有合理的理由为什么'anyOf'不是'params'?



我的第一个想法是它可能会导致重载过多,因为在a之后不再允许参数'PARAMS'。存在IndexOfAny重载,参数尾随'anyOf'。也许这会让编译器感到困惑。



编号String.Split()有一个'params char []'重载,其他有附加参数(但对于那些,'对于第一个'separator'参数,省略了params'。如果编译器可以处理Split(),为什么它不能处理IndexOfAny()和LastIndexOfAny()的相同构造?



程序员是否忘记了params,还是有理性/技术原因?



< b>我尝试了什么:



提到的功能声明在VS.元数据显示中可用。

Not a problem, just a question:

You may write: trimmed = untrimmed.Trim('(', ')', '[', ']');
But you can't write: pos = line.IndexOfAny('(', ')', '[', ']');

... 'trimChars' is a 'params char[]', 'anyOf' is a 'char[]' (without 'params'). You must write: pos = line.IndexOfAny(new char[] {'(', ')', '[', ']')};

Is there any rational reason why 'anyOf' is not a 'params'?

My first thought was that it might mess up overloading, since no more parameters are allowed after a 'params'. There are IndexOfAny overloads with parameters trailing 'anyOf'. Maybe this would confuse the compiler.

No. String.Split() has one 'params char[]' overload, others with additional parameters (but for those, 'params' is omitted for the first, 'separator', parameter). If the compiler can handle Split(), why couldn't it handle the same constructions for IndexOfAny() and LastIndexOfAny()?

Did the programmer just forget about params, or is there a rational/technical reason?

What I have tried:

Function declarations mentioned are available in the metadata display in VS.

推荐答案

引用:

为什么'anyOf'不是'params'有什么理由吗?

Is there any rational reason why 'anyOf' is not a 'params'?

可能,但微软将是唯一真正了解的人。您可以在文档页面上添加反馈,或查看MSDN上的语言论坛。毕竟,他们是专家。

Possibly, but Microsoft will be the only people who really know. You can add feedback on the documentation pages, or check the language forums at MSDN. After all, they are the experts.


这篇关于为什么一些字符串函数用“params”声明,其他字符串不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆