可能重复:
Why does “abcd”.StartsWith(“”) return true?错误在C#中 - “FooBar的” .StartsWith(的String.Empty)为真
类似的东西这事是否真实抓到我们了:
"FooBar".StartsWith(string.Empty)
首先我不认为它应该是真的,但我也不太确定它为什么是真的,看着“Reflector'ed”代码:
public bool StartsWith(string value, bool ignoreCase, CultureInfo culture)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (this == value)
{
return true;
}
CultureInfo info = (culture == null) ? CultureInfo.CurrentCulture : culture;
return info.CompareInfo.IsPrefix(this, value, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None);
}
ignoreCase为false且文化为空。那么为什么“这个==值”评估为真?
退房http://stackoverflow.com/questions/145509/why-does-abcd-startswith-return-true/145516#145516 – 2011-03-07 14:36:20
是的,我刚刚发现的问题,谢谢。 – NetRunner 2011-03-07 14:37:03