设为首页 加入收藏

TOP

c# Equal函数 and 运算符'==' (原发布 csdn 2017年10月15日 20:39:26)(四)
2019-09-17 15:14:57 】 浏览:55
Tags:Equal 函数 and 运算 ' 发布 csdn 2017年 10月 15日 20:39:26
p1; p2.rPoint = rPoint2; Console.WriteLine(p1.Equals(p2)); //返回false Console.WriteLine(p1.Equals((object)p2)); //返回false

3.2.5.2、程序执行时,CanCompareBits(this)返回false,代码执行ValueType类Equal函数的下面语句

                FieldInfo[] thisFields = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
     
                for (int i=0; i<thisFields.Length; i++) {
                    thisResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(thisObj);
                    thatResult = ((RtFieldInfo)thisFields[i]).UnsafeGetValue(obj);
                    
                    if (thisResult == null) {
                        if (thatResult != null)
                            return false;
                    }
                    else
                    if (!thisResult.Equals(thatResult)) {
                        return false;
                    }
                }
     
                return true;

3.2.5.3、结论:程序判断struct里面所有字段,值类型就判断值是否相等;引用类型就判断是否引用相等。

4、引用类型Equal函数 and 运算符'=='

4.1、字符串string

4.1.1、C#语言规范5.0中文版中的字符串相等运算符介绍

这里写图片描述

4.1.2、string的Equal函数和'=='重载运算符函数代码

        String
        {
            // Determines whether two strings match.
            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
            public override bool Equals(Object obj) {
                if (this == null)                        //this is necessary to guard against reverse-pinvokes and
                    throw new NullReferenceException();  //other callers who do not use the callvirt instruction
     
                String str = obj as String;
                if (str == null)
                    return false;
     
                if (Object.ReferenceEquals(this, obj))
                    return true;
     
                if (this.Length != str.Length)
                    return false;
     
                return EqualsHelper(this, str);
            }
     
            // Determines whether two strings match.
            [Pure]
            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
            public bool Equals(String value) {
                if (this == null)                        //this is necessary to guard against reverse-pinvokes and
                    throw new NullReferenceException();  //other callers who do not use the callvirt instruction
     
                if (value == null)
                    return false;
     
                if (Object.ReferenceEquals(this, value))
                    return true;
                
                if (this.Length != value.Length)
                    return false;
     
                return EqualsHelper(this, value);
            }

            public static bool operator == (String a, String b) {
               return String.Equals(a, b);
            }

            // Determines whether two Strings match.
            [Pure]
            public static bool Equals(String a, String b) {
                if ((Object)a==(Object)b) {
                    return true;
                }
     
                if ((Object)a==null || (Object)b==null) {
                    return false;
                }
     
                if (a.Length != b.Length)
                    return false;
     
                return EqualsHelper(a, b);
            }

            [System.Security.SecuritySafeCritical]  // auto-generated
            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
            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;
     
                fixed (char* ap = &strA.m_firstChar) fixed (char* bp = &strB.m_firstChar)
                {
                    char* a = ap;
                    char* b = bp;
     
                    // unroll the loop
    #if AMD64
                    // for AMD64 bit platform we unroll by 12 and
                    // check 3 qword at a time. This is less code
                    // than the 32 bit case and is shorter
                    // pathlength
     
                    while (length >= 12)
                    {
                        if (*(long*)a     != *(long*)b) return false;
                        if (*(long*)(a+4) != *(long*)(b+4)) return false;
                        if (*(long*)(a+8) != *(long*)(b+8)) return false;
                        a += 12; b += 12; length -= 12;
                    }
    #else
                    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;
                    }
    #endif
     
                    // This de
首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇asp.net core 之中间件 下一篇c# "As" 与 "Is&q..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目