我正在通過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不同? –