current position:Home>C compare whether the values of two objects are the same

C compare whether the values of two objects are the same

2022-05-15 07:49:33Haihong AA

#region // Whether the two values are equal 
        /// <summary>
        ///  Compare the two object Whether objects have equal values , use Equal and RefrenceEqual perhaps == Can't compare directly 
        ///  It can be used static class Expand object type 
        /// </summary>
        /// <param name="a"> The number a</param>
        /// <param name="b"> The number b</param>
        /// <param name=" type ">S,N,D,L,O Several types </param>
        /// <returns></returns>
        public static bool  Whether the two values are equal (object a, object b, string  type  = "")
        {
            bool result = false, l1 = false, l2 = false;
            if ((l1 = (a == null || a == DBNull.Value)) && (l2 = (b == null || b == DBNull.Value))) return true;  // All two are null
            if (l1 != l2) return false;     // One is null One is not null
            // Judge according to the type of input 
            string sType =  type .ToUpper();
            if (sType == "N") return Convert.ToDouble(a) == Convert.ToDouble(b);
            if (sType == "S") return a.ToString() == b.ToString();
            if (sType == "D") return Convert.ToDateTime(a) == Convert.ToDateTime(b);
            if (sType == "L") return Convert.ToBoolean(a) == Convert.ToBoolean(b);
            if (sType != "") return object.Equals(a, b);
            // Is it a number 
            if (GsDefineTypes.isNumber(a.GetType()) && GsDefineTypes.isNumber(b.GetType())) return Convert.ToDouble(a) == Convert.ToDouble(b);
            return object.Equals(a, b);
        }
        #endregion


        #region //getDefaultValueOfType: Take the default value of type 
        /// <summary>
        ///  Take the default value of type , Than default(T) Fortunately, it is easy to control 
        /// </summary>
        /// <param name="type"> The type to take </param>
        /// <returns> Return default ,object Type return DBNull</returns>
        public static object getDefaultValueOfType(Type type)
        {   // Take the default value of type 
            object result = null;
            if (isNumber(type)) result = 0;
            else if (type == typeof(System.Boolean) || type == typeof(System.Boolean?)) result = false;
            else if (type == typeof(System.Byte)) result = 1;
            else if (type == typeof(System.SByte)) result = 1;
            else if (type == typeof(System.Char)) result = "";
            else if (type == typeof(System.String)) result = "";
            // Other types 
            else if (type == typeof(System.DateTime) || type == typeof(System.DateTime?)) result = DateTime.Today;
            else if (type == typeof(System.Guid)) result = Guid.NewGuid();
            // None of them , Just the base class 
            else if (type == typeof(System.Object)) result = null;
            //
            return result;
        }
        #endregion


        #region // It's the number type isNumber
        public static bool isNumber(Type type)
        {
            bool result = false;
            if (type == typeof(System.Decimal) || type == typeof(System.Decimal?)) result = true;
            else if (type == typeof(System.Double) || type == typeof(System.Double?)) result = true;
            else if (type == typeof(System.Single) || type == typeof(System.Single?)) result = true;
            else if (type == typeof(System.Int16) || type == typeof(System.Int16?)) result = true;
            else if (type == typeof(System.UInt16) || type == typeof(System.UInt16?)) result = true;
            else if (type == typeof(System.Int32) || type == typeof(System.Int32?)) result = true;
            else if (type == typeof(System.UInt32) || type == typeof(System.UInt32?)) result = true;
            else if (type == typeof(System.Int64) || type == typeof(System.Int64?)) result = true;
            else if (type == typeof(System.UInt64) || type == typeof(System.UInt64?)) result = true;
            //
            return result;
        }
        #endregion

Direct use ==、equal、refrenceEqual Judgment is not good .

For example, a int And a double All are 0, direct object.equal Return is not equal , Because one is int One double.

copyright notice
author[Haihong AA],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102049079936.html

Random recommended