[英]Why does every Char static “Is…” have a string overload, e.g. IsWhiteSpace(string, Int32)?
http://msdn.microsoft.com/en-us/library/1x308yk8.aspx
這允許我這樣做:
var str = "string ";
Char.IsWhiteSpace(str, 6);
而不是:
Char.IsWhiteSpace(str[6]);
看起來很不尋常,所以我看了一下反思:
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsWhiteSpace(char c)
{
if (char.IsLatin1(c))
{
return char.IsWhiteSpaceLatin1(c);
}
return CharUnicodeInfo.IsWhiteSpace(c);
}
[SecuritySafeCritical]
public static bool IsWhiteSpace(string s, int index)
{
if (s == null)
{
throw new ArgumentNullException("s");
}
if (index >= s.Length)
{
throw new ArgumentOutOfRangeException("index");
}
if (char.IsLatin1(s[index]))
{
return char.IsWhiteSpaceLatin1(s[index]);
}
return CharUnicodeInfo.IsWhiteSpace(s, index);
}
三件事讓我印象深刻:
ArgumentOutOfRangeException
,而索引低於0將給出字符串的標准IndexOutOfRangeException
SecuritySafeCriticalAttribute
的前提,我已經閱讀了一般的blerb,但仍然不清楚它在這里做了什么以及它是否與上限檢查相關聯。 Is...(char)
方法中不存在TargetedPatchingOptOutAttribute
。 示例IsLetter
, IsNumber
等 為什么只在上限進行限制檢查呢?
它沒有。 它執行無符號比較,因此每個負數將比長度大,並導致拋出相應的異常。 這恰好不能准確地反編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.