设为首页 加入收藏

TOP

c# Equal函数 and 运算符'==' (原发布 csdn 2017年10月15日 20:39:26)(五)
2019-09-17 15:14:57 】 浏览:59
Tags:Equal 函数 and 运算 ' 发布 csdn 2017年 10月 15日 20:39:26
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
首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇asp.net core 之中间件 下一篇c# "As" 与 "Is&q..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目