pends on the fact that the String objects are
// always zero terminated and that the terminating zero is not included
// in the length. For odd string sizes, the last compare will include
// the zero terminator.
while (length > 0)
{
if (*(int*)a != *(int*)b) break;
a += 2; b += 2; length -= 2;
}
return (length <= 0);
}
}
}
4.1.3、Object.ReferenceEquals(this, value)如果this、value是同一个引用,返回true;否则,返回false。
{
string a = "a1!";
string b = "a1!";
Console.WriteLine(Object.ReferenceEquals(a, b));//返回true,可以判断编译器将a与b所指向的"a1!"优化成一个地方。
}
{
string a = "Test";
string b = string.Copy(a);
Console.WriteLine(Object.ReferenceEquals(a, b));//返回false
}
{
string a = "Test";
string b = (string)a.Clone();
Console.WriteLine(Object.ReferenceEquals(a, b));//返回true
}
{
char[] ch = new char[] { 'a', 'A', '@' };
string a = "aA@";
string b = new string(ch);
Console.WriteLine(Object.ReferenceEquals(a, b));//返回false
}
4.1.4、学习EqualsHelper(String strA, String strB)函数之前,我们先看一段代码
unsafe
{
char[] firstCharA = "abc".ToCharArray();
int length = firstCharA.Length;
fixed (char* ap = firstCharA)
{
for (int i = 0; i < length; i++)
{
Console.WriteLine(*(char*)(ap + i));
}
}
}
unsafe
{
int[] firstCharA = new int[] { 1, 20, 300 };
int length = firstCharA.Length;
fixed (int* ap = firstCharA)
{
for (int i = 0; i < length; i++)
{
Console.WriteLine(*(int*)(ap + i));
}
}
}
4.1.5、修改后EqualsHelper(String strA, String strB)函数
private unsafe static bool EqualsHelper(String strA, String strB)
{
Contract.Requires(strA != null);
Contract.Requires(strB != null);
Contract.Requires(strA.Length == strB.Length);
int length = strA.Length;
char[] firstCharA = strA.ToCharArray();
char[] firstCharB = strB.ToCharArray();
fixed (char* ap = &firstCharA[0]) fixed (char* bp = &firstCharB[0])//因无法使用m_firstChar,此处是我自行修改。ps:个人认为m_firstChar是指字符串的第一字符,但是无法证明。
//fixed (char* ap = &strA.m_firstChar) fixed (char* bp = &strB.m_firstChar)
{
char* a = ap;
char* b = bp;
while (length >= 10)
{
if (*(int*)a != *(int*)b) return false;
if (*(int*)(a + 2) != *(int*)(b + 2)) return false;
if (*(int*)(a + 4) != *(int*)(b + 4)) return false;
if (*(int*)(a + 6) != *(int*)(b + 6)) return false;
if (*(int*)(a + 8) != *(int*)(b + 8)) return false;
a += 10; b += 10; length -= 10;
}
// This depends on the fact that the String objects are
// always zero terminated and that the terminating zero is not included
// in the length. For odd string sizes, the last compare will include
// the zero terminator.
while (length > 0)
{
if (*(int*)a != *(int*)b) break;
a += 2; b += 2; length -= 2;
}
return (length <= 0);
}
}
4.1.6、修改说明
1、fixed (char* ap = &strA.m_firstChar) fixed (char* bp = &strB.m_firstChar)-> fixed (char* ap = &firstCharA[0]) fixed (char* bp = &firstCharB[0])
2、(*(int*)a->获取的数据是两个char值(低位ASCII*65536+高位ASCII)[低位在前,高位在后]。 [char两个字节,范围U+0000到U+FFFF]
3、(*(char*)a->获取的数据是一个char值[见上面测试例子]
4.1.7、测试EqualsHelper(String strA, String strB)函数
{
string a = "abcd";
string b = "abcd";
Console.WriteLine(EqualsHelper(a,b));//返回true
}
{
string a = "Test";
string b = string.Copy(a);
Console.WriteLine(EqualsHelper(a, b));//返回true
}
{
string a = "Test";
string b = (string)a.Clone();
Console.WriteLine(Equ