设为首页 加入收藏

TOP

c# Equal函数 and 运算符'==' (原发布 csdn 2017年10月15日 20:39:26)(一)
2019-09-17 15:14:57 】 浏览:53
Tags:Equal 函数 and 运算 ' 发布 csdn 2017年 10月 15日 20:39:26

1、==、!=、<、>、<= 和>= 运算符为比较运算符(comparison operator)。C#语言规范5.0中文版中比较运算符的描述如下:

这里写图片描述

2、通用类型系统

这里写图片描述

3、值类型Equal函数 and 运算符'=='

3.1、常见类型 int、float、double、decimal等虽然继承自ValueType,但其结构体内部重写了Equal。

3.1.1、 int,float,double,decimal内部的Equal函数和 '=='重载符函数。

        Int32
        {
            public override bool Equals(Object obj) {
                if (!(obj is Int32)) {
                    return false;
                }
                return m_value == ((Int32)obj).m_value;
            }
     
            [System.Runtime.Versioning.NonVersionable]
            public bool Equals(Int32 obj)
            {
                return m_value == obj;
            }           
        }

        Double
        {
            // True if obj is another Double with the same value as the current instance.  This is
            // a method of object equality, that only returns true if obj is also a double.
            public override bool Equals(Object obj) {
                if (!(obj is Double)) {
                    return false;
                }
                double temp = ((Double)obj).m_value;
                // This code below is written this way for performance reasons i.e the != and == check is intentional.
                if (temp == m_value) {
                    return true;
                }
                return IsNaN(temp) && IsNaN(m_value);
            }
     
            public bool Equals(Double obj)
            {
                if (obj == m_value) {
                    return true;
                }
                return IsNaN(obj) && IsNaN(m_value);
            }    

            [System.Runtime.Versioning.NonVersionable]
            public static bool operator ==(Double left, Double right) {
                return left == right;
            }           
        }

        Single
        {
            public override bool Equals(Object obj) {
                if (!(obj is Single)) {
                    return false;
                }
                float temp = ((Single)obj).m_value;
                if (temp == m_value) {
                    return true;
                }
     
                return IsNaN(temp) && IsNaN(m_value);
            }
     
            public bool Equals(Single obj)
            {
                if (obj == m_value) {
                    return true;
                }
     
                return IsNaN(obj) && IsNaN(m_value);
            }

             [System.Runtime.Versioning.NonVersionable]
            public static bool operator ==(Single left, Single right) {
                return left == right;
            }           
        }

        Decimal
        {
            // Checks if this Decimal is equal to a given object. Returns true
            // if the given object is a boxed Decimal and its value is equal to the
            // value of this Decimal. Returns false otherwise.
            //
            [System.Security.SecuritySafeCritical]  // auto-generated
            public override bool Equals(Object value) {
                if (value is Decimal) {
                    Decimal other = (Decimal)value;
                    return FCallCompare(ref this, ref other) == 0;
                }
                return false;
            }
     
            [System.Security.SecuritySafeCritical]  // auto-generated
            public bool Equals(Decimal value)
            {
                return FCallCompare(ref this, ref value) == 0;
            }   

            [System.Security.SecuritySafeCritical]  // auto-generated
            public static bool operator ==(Decimal d1, Decimal d2) {
                return FCallCompare(ref d1, ref d2) == 0;
            }

            //暂时不知道此函数内部代码,如有知道还望告知。
            //根据测试结果,推测如果两个decimal数相等,返回0
            [System.Security.SecurityCritical]  // auto-generated
            [ResourceExposure(ResourceScope.None)]
            [MethodImplAttribute(MethodImplOptions.InternalCall)]
            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
            private static extern int FCallCompare(ref Decimal d1, ref Decimal d2);    

        }

3.1.2、感兴趣的可去Reference Source查看全部代码。

3.1.3、测试代码:

            //T is int 、float、double、decimal、byte、char
            T a = 1234567890;//0.1234567890f、0.123456789、1234567890M、(byte)11、'a'
            T b = 1234567890;//0.1234567890f、0.123456789、1234567890M、(byte)11、'a'
    
            Console.WriteLine(a == b);//返回true
            Console.WriteLine(a.Equals(b));//返回true
            Console.WriteLine(a.Equals((object)b));//返回true
            
            /*
            Console.WriteLine((object)a == b);//编译错误:运算符‘==’无法应用与‘object’和‘T’类型操作数
            Console.WriteLine(a == (object)b);//编译错误:运算符‘==’无法应用与‘object’和‘T’类型操作数
            //Console.WriteLine((obj
首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇asp.net core 之中间件 下一篇c# "As" 与 "Is&q..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目