我正在通过System.String
寻找,我想知道为什么EndsWith
和StartsWith
方法在参数方面不对称。为什么System.String.EndsWith()有一个char重载并且System.String.StartsWith()没有?
具体来说,为什么System.String.EndsWith
支持char参数,而System.String.StartsWith
不支持?这是因为任何限制或设计功能?
// System.String.EndsWith method signatures
[__DynamicallyInvokable]
public bool EndsWith(string value)
[ComVisible(false)]
[SecuritySafeCritical]
[__DynamicallyInvokable]
public bool EndsWith(string value, StringComparison comparisonType)
public bool EndsWith(string value, bool ignoreCase, CultureInfo culture)
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
internal bool EndsWith(char value)
{
int length = this.Length;
return length != 0 && (int) this[length - 1] == (int) value;
}
// System.String.StartsWith method signatures
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
[__DynamicallyInvokable]
public bool StartsWith(string value)
[SecuritySafeCritical]
[ComVisible(false)]
[__DynamicallyInvokable]
public bool StartsWith(string value, StringComparison comparisonType)
public bool StartsWith(string value, bool ignoreCase, CultureInfo culture)
的'StartsWith'答案是有道理的。我不确定这个答案的第一部分是否对我有意义 - 当我使用dotPeek时,我实际上得到了上面的输出。在Path.DirectorySeparatorChar中调用'EndsWith'passing是什么意思? –
我的意思是......看起来好像编写IO代码的人注意到他们需要检查字符串的最后一个字符,所以他们只写了一个'internal'辅助方法来使它更容易? – Rawling
我现在明白了 - 结合下面的答案是有道理的。我想知道为什么ILSpy显示的代码与dotPeek不同? –