码迷,mamicode.com
首页 > Windows程序 > 详细

C# 中 ==和equals的区别

时间:2019-05-31 10:33:25      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:source   family   flow   col   provider   max   不同   parse   比较   

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

下面是测试代码

            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);    }

C# 中 ==和equals的区别

标签:source   family   flow   col   provider   max   不同   parse   比较   

原文地址:https://www.cnblogs.com/tanl/p/10953670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!