C#值类型、引用类型中的Equals和==的区别浅析

2019-12-26 11:10:51王旭

                    return false;
                }
                if (a.IsAscii() && b.IsAscii())
                {
                    return string.CompareOrdinalIgnoreCaseHelper(a, b) == 0;
                }
                return TextInfo.CompareOrdinalIgnoreCase(a, b) == 0;
            default:
                throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), "comparisonType");
            }
        }
        [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator ==(string a, string b)
        {
            return string.Equals(a, b);
        }
        [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator !=(string a, string b)
        {
            return !string.Equals(a, b);
        }

 

从上面的代码可以看出string类型的Equals和==是一样的。

 

复制代码
public static bool operator ==(string a, string b)
        {
            return string.Equals(a, b);
        }
[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
        public static bool operator !=(string a, string b)