[英]Why does every Char static “Is…” have a string overload, e.g. IsWhiteSpace(string, Int32)?
[英]Why does System.String.EndsWith() have a a char overload and System.String.StartsWith() does not?
我正在查看System.String
,我想知道为什么EndsWith
和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)
看看ILSpy,这个重载似乎压倒性地在IO代码中调用
s.EndsWith(Path.DirectorySeparatorChar)
据推测,这只是C#团队认为必须避免重复代码有用的东西。
请注意,在开始时进行此检查要容易得多( s[0] == c
vs s[s.Length - 1] == c
),这也可以解释为什么他们不打算使StartsWith
超载。
这是一个内部方法,仅用于mscorlib
中的以下8个方法:
可能只是为了方便和代码重用:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.