不想说太多,直接上代码,这两个就没什么联系,有自己独立的规则。比较其实不利于记忆。

下面是测试代码

            console.writeline("--equals和==的区别--");
            console.writeline("1、对于值类型的数据");
            console.writeline();
            console.writeline("1.1、类型和数学上的值都相同的数");
            console.writeline($"(int)1 == (int)1 的结果是:{(int)1 == (int)1}");
            console.writeline($"(int)1).equals((int)1) 的结果是:{((int)1).equals((int)1)}");

            console.writeline("1.2、对于类型不同但是数学上值相同的数");
            console.writeline($"(int)1 == (long)1 的结果是:{(int)1 == (long)1}");
            console.writeline($"(int)1 == (double)1 的结果是:{(int)1 == (double)1}");
            console.writeline($"(int)1 == (double)1.0 的结果是:{(int)1 == (double)1.0}");
            console.writeline($"(int)1 == (decimal)1 的结果是:{(int)1 == (decimal)1}");
            console.writeline($"(int)1 == (decimal)1.0 的结果是:{(int)1 == (decimal)1.0}");
            console.writeline($"((int)1).equals((long)1 的结果是:{((int)1).equals((long)1)}");
            console.writeline();

            console.writeline("2、对于引用类型的数据");
            console.writeline();
            console.writeline("2.1、在1.1的基础上转化为object再比较");
            console.writeline($"(object)(int)1 == (object)(int)1 的结果是:{(object)(int)1 == (object)(int)1}");
            console.writeline($"((object)(int)1).equals((object)(int)1 的结果是:{((object)(int)1).equals((object)(int)1)}");
            console.writeline();

            console.writeline("2.2、在1.2的基础上转化为object再比较");
            console.writeline($"(object)(int)1 == (object)(long)1 的结果是:{(object)(int)1 == (object)(long)1}");
            console.writeline($"((object)(int)1).equals((object)(long)1 的结果是:{((object)(int)1).equals((object)(long)1)}");
            console.writeline();
            console.readkey();

 

打印如下:

–equals和==的区别–
1、对于值类型的数据

1.1、类型和数学上的值都相同的数
(int)1 == (int)1 的结果是:true
(int)1).equals((int)1) 的结果是:true
1.2、对于类型不同但是数学上值相同的数
(int)1 == (long)1 的结果是:true
(int)1 == (double)1 的结果是:true
(int)1 == (double)1.0 的结果是:true
(int)1 == (decimal)1 的结果是:true
(int)1 == (decimal)1.0 的结果是:true
((int)1).equals((long)1 的结果是:false

2、对于引用类型的数据

2.1、在1.1的基础上转化为object再比较
(object)(int)1 == (object)(int)1 的结果是:false
((object)(int)1).equals((object)(int)1 的结果是:true

2.2、在1.2的基础上转化为object再比较
(object)(int)1 == (object)(long)1 的结果是:false
((object)(int)1).equals((object)(long)1 的结果是:false

 

 

还是给个说明吧:

equals具体的用发应该看类型自己的实现。

对于int型的比较代码是这样定义的:如果比较的是int型,返回使用==比较的结果;如果不是,先判断是不是int型,不是直接返回false,是再返回使用==比较的结果。

int 的equals方法只支持int和object两种,如果传入其他类型,会自动转化为object。如((int)1).equals((long)1 等价于 ((int)1).equals((object)(long)1

        [__dynamicallyinvokable]
        public override bool equals(object obj)
        {
            if (!(obj is int))
            {
                return false;
            }
            return this == (int)obj;
        }

        [nonversionable]
        [__dynamicallyinvokable]
        public bool equals(int obj)
        {
            return this == obj;
        }

 

    //    // 摘要:    //     表示 32 位有符号整数。 若要浏览此类型的.net framework 源代码,请参阅 reference source。    [comvisible(true)]    public struct int32 : icomparable, iformattable, iconvertible, icomparable<int32>, iequatable<int32>    {        //        // 摘要:        //     表示 system.int32 的最大可能值。 此字段为常数。        public const int32 maxvalue = 2147483647;        //        // 摘要:        //     表示 system.int32 的最小可能值。 此字段为常数。        public const int32 minvalue = -2147483648;
        //        // 摘要:        //     将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 32 位有符号整数。        //        // 参数:        //   s:        //     包含要转换的数字的字符串。        //        //   style:        //     枚举值的按位组合,用于指示可出现在 s 中的样式元素。 要指定的一个典型值为 system.globalization.numberstyles.integer。        //        //   provider:        //     一个对象,用于提供有关 s 格式的区域性特定信息。        //        // 返回结果:        //     与 s 中指定的数字等效的 32 位带符号整数。        //        // 异常:        //   t:system.argumentnullexception:        //     s 为 null。        //        //   t:system.argumentexception:        //     style 不是 system.globalization.numberstyles 值。 – 或 – style 不是 system.globalization.numberstyles.allowhexspecifier        //     和 system.globalization.numberstyles.hexnumber 值的组合。        //        //   t:system.formatexception:        //     s 的格式不符合 style。        //        //   t:system.overflowexception:        //     s 表示一个小于 system.int32.minvalue 或大于 system.int32.maxvalue 的数字。 – 或 – s 包含非零的小数位。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public static int32 parse(string s, numberstyles style, iformatprovider provider);        //        // 摘要:        //     将指定的区域性特定格式的数字的字符串表示形式转换为它的等效 32 位有符号整数。        //        // 参数:        //   s:        //     包含要转换的数字的字符串。        //        //   provider:        //     一个对象,提供有关 s 的区域性特定格式设置信息。        //        // 返回结果:        //     与 s 中指定的数字等效的 32 位带符号整数。        //        // 异常:        //   t:system.argumentnullexception:        //     s 为 null。        //        //   t:system.formatexception:        //     s 的格式不正确。        //        //   t:system.overflowexception:        //     s 表示一个小于 system.int32.minvalue 或大于 system.int32.maxvalue 的数字。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public static int32 parse(string s, iformatprovider provider);        //        // 摘要:        //     将数字的字符串表示形式转换为它的等效 32 位有符号整数。        //        // 参数:        //   s:        //     包含要转换的数字的字符串。        //        // 返回结果:        //     与 s 中包含的数字等效的 32 位有符号整数。        //        // 异常:        //   t:system.argumentnullexception:        //     s 为 null。        //        //   t:system.formatexception:        //     s 的格式不正确。        //        //   t:system.overflowexception:        //     s 表示一个小于 system.int32.minvalue 或大于 system.int32.maxvalue 的数字。        public static int32 parse(string s);        //        // 摘要:        //     将指定样式的数字的字符串表示形式转换为它的等效 32 位有符号整数。        //        // 参数:        //   s:        //     包含要转换的数字的字符串。        //        //   style:        //     枚举值的按位组合,用于指示可出现在 s 中的样式元素。 要指定的一个典型值为 system.globalization.numberstyles.integer。        //        // 返回结果:        //     与 s 中指定的数字等效的 32 位带符号整数。        //        // 异常:        //   t:system.argumentnullexception:        //     s 为 null。        //        //   t:system.argumentexception:        //     style 不是 system.globalization.numberstyles 值。 – 或 – style 不是 system.globalization.numberstyles.allowhexspecifier        //     和 system.globalization.numberstyles.hexnumber 值的组合。        //        //   t:system.formatexception:        //     s 的格式不符合 style。        //        //   t:system.overflowexception:        //     s 表示一个小于 system.int32.minvalue 或大于 system.int32.maxvalue 的数字。 – 或 – s 包含非零的小数位。        public static int32 parse(string s, numberstyles style);        //        // 摘要:        //     将数字的字符串表示形式转换为它的等效 32 位有符号整数。 一个指示转换是否成功的返回值。        //        // 参数:        //   s:        //     包含要转换的数字的字符串。        //        //   result:        //     当此方法返回时,如果转换成功,则包含与 s 中所包含的数字等效的 32 位无符号整数值;如果转换失败,则包含零。 如果 s 参数为 null 或 system.string.empty、格式不正确,或者表示的数字小于        //     system.int32.minvalue 或大于 system.int32.maxvalue,则转换失败。 此参数未经初始化即进行传递;最初在 result        //     中提供的任何值都会被覆盖。        //        // 返回结果:        //     如果 true 成功转换,则为 s;否则为 false。        public static bool tryparse(string s, out int32 result);        //        // 摘要:        //     将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 32 位有符号整数。 一个指示转换是否成功的返回值。        //        // 参数:        //   s:        //     包含要转换的数字的字符串。 该字符串使用由 style 指定的样式来进行解释。        //        //   style:        //     枚举值的按位组合,用于指示可出现在 s 中的样式元素。 要指定的一个典型值为 system.globalization.numberstyles.integer。        //        //   provider:        //     一个对象,提供有关 s 的区域性特定格式设置信息。        //        //   result:        //     当此方法返回时,如果转换成功,则包含与 s 中所包含的数字等效的 32 位无符号整数值;如果转换失败,则包含零。 如果 s 参数为 null 或 system.string.empty、格式不符合        //     style,或者表示的数字小于 system.int32.minvalue 或大于 system.int32.maxvalue,则转换失败。 此参数未经初始化即进行传递;最初在        //     result 中提供的任何值都会被覆盖。        //        // 返回结果:        //     如果 true 成功转换,则为 s;否则为 false。        //        // 异常:        //   t:system.argumentexception:        //     style 不是 system.globalization.numberstyles 值。 – 或 – style 不是 system.globalization.numberstyles.allowhexspecifier        //     和 system.globalization.numberstyles.hexnumber 值的组合。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public static bool tryparse(string s, numberstyles style, iformatprovider provider, out int32 result);        //        // 摘要:        //     将此实例与指定对象进行比较并返回一个对二者的相对值的指示。        //        // 参数:        //   value:        //     要比较的对象,或为 null。        //        // 返回结果:        //     一个带符号数字,指示此实例和 value 的相对值。 返回值 描述 小于零 此实例小于 value。 零 此实例等于 value。 大于零 此实例大于 value。        //     – 或 – value 为 null。        //        // 异常:        //   t:system.argumentexception:        //     value 不是 system.int32。        public int32 compareto(object value);        //        // 摘要:        //     将此实例与指定的 32 位有符号整数进行比较并返回对其相对值的指示。        //        // 参数:        //   value:        //     要比较的整数。        //        // 返回结果:        //     一个带符号数字,指示此实例和 value 的相对值。 返回值 描述 小于零 此实例小于 value。 零 此实例等于 value。 大于零 此实例大于 value。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public int32 compareto(int32 value);        //        // 摘要:        //     返回一个值,该值指示此实例是否等于指定的对象。        //        // 参数:        //   obj:        //     与此实例进行比较的对象。        //        // 返回结果:        //     如果 true 是 obj 的实例并且等于此实例的值,则为 system.int32;否则为 false。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public override bool equals(object obj);        //        // 摘要:        //     返回一个值,该值指示此实例是否等于指定的 system.int32 值。        //        // 参数:        //   obj:        //     要与此实例进行比较的 system.int32 值。        //        // 返回结果:        //     如果 true 的值与此实例相同,则为 obj;否则为 false。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public bool equals(int32 obj);        //        // 摘要:        //     返回此实例的哈希代码。        //        // 返回结果:        //     32 位有符号整数哈希代码。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public override int32 gethashcode();        //        // 摘要:        //     返回值类型 system.typecode 的 system.int32。        //        // 返回结果:        //     枚举常数 system.typecode.int32。        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public typecode gettypecode();        //        // 摘要:        //     使用指定的区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。        //        // 参数:        //   provider:        //     一个提供区域性特定的格式设置信息的对象。        //        // 返回结果:        //     此实例的值的字符串表示形式,由 provider 指定。        [securitysafecritical]        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public string tostring(iformatprovider provider);        //        // 摘要:        //     将此实例的数值转换为其等效的字符串表示形式。        //        // 返回结果:        //     此实例的值的字符串表示形式,由减号(如果值为负)和没有前导零的从 0 到 9 的数字序列组成。        [securitysafecritical]        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public override string tostring();        //        // 摘要:        //     使用指定的格式和区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。        //        // 参数:        //   format:        //     标准或自定义的数值格式字符串。        //        //   provider:        //     一个提供区域性特定的格式设置信息的对象。        //        // 返回结果:        //     此实例的值的字符串表示形式,由 format 和 provider 指定。        //        // 异常:        //   t:system.formatexception:        //     format 无效或不受支持。        [securitysafecritical]        public string tostring(string format, iformatprovider provider);        //        // 摘要:        //     使用指定的格式,将此实例的数值转换为它的等效字符串表示形式。        //        // 参数:        //   format:        //     标准或自定义的数值格式字符串。        //        // 返回结果:        //     此实例的值的字符串表示形式,由 format 指定。        //        // 异常:        //   t:system.formatexception:        //     format 无效或不受支持。        [securitysafecritical]        [targetedpatchingoptout(“performance critical to inline across ngen image boundaries”)]        public string tostring(string format);    }