![](/img/trans.png)
[英]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.