http://msdn.microsoft.com/en-us/library/1x308yk8.aspx
这允许我这样做:
var str = "string ";
Char.IsWhiteSpace(str, 6);
Run Code Online (Sandbox Code Playgroud)
而不是:
Char.IsWhiteSpace(str[6]);
Run Code Online (Sandbox Code Playgroud)
看起来很不寻常,所以我看了一下反思:
[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);
}
Run Code Online (Sandbox Code Playgroud)
三件事让我印象深刻:
ArgumentOutOfRangeException,而索引低于0将给出字符串的标准IndexOutOfRangeExceptionSecuritySafeCriticalAttribute我读过关于一般blerb,但在这里做什么还不清楚,如果它链接到上限检查.TargetedPatchingOptOutAttribute其他Is...(char)方法不存在.例如IsLetter,IsNumber等