首頁 >後端開發 >C#.Net教程 >C# 物件比較(值型別、參考型別)

C# 物件比較(值型別、參考型別)

黄舟
黄舟原創
2017-02-15 11:12:241567瀏覽

<pre class="csharp"><pre class="csharp">        #region 引用对象比较
        /// <summary>
        /// 引用对象比较
        /// </summary>
        /// <param name="objA"></param>
        /// <param name="objB"></param>
        /// <returns></returns>
        public static bool CompareObject(object objA, object objB)
        {
            bool flag = false;
            if (objA == null || objB == null)
            {
                flag = false;
            }
            else if (objA == DBNull.Value && objB != DBNull.Value)
            {
                flag = false;
            }
            else if (objA != DBNull.Value && objB == DBNull.Value)
            {
                flag = false;
            }
            else if (objA == DBNull.Value && objB == DBNull.Value)
            {
                //objA objB 对应的列类型已经比较过 类型已判断 值一致
                flag = true;
            }
            else if (objA.GetType() != objB.GetType())
            {
                flag = false;
            }
            else if (objA is int || objA is short || objA is long || objA is float || objA is double || objA is decimal)
            {
                //int 01与1      
                if (objA is int)
                {
                    if ((int)objA == (int)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is short)
                {
                    if ((short)objA == (short)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is long)
                {
                    if ((long)objA == (long)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is float)
                {
                    if ((float)objA == (float)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is double)
                {
                    if ((double)objA == (double)objB)
                    {
                        flag = true;
                    }
                }
                else if (objA is decimal)
                {
                    if ((decimal)objA == (decimal)objB)
                    {
                        flag = true;
                    }
                }
            }
            else
            {
                string strA = MetadataXmlSerializer<object>.ToXMLString(objA);
                string strB = MetadataXmlSerializer<object>.ToXMLString(objB);
                if (strA == strB)
                {
                    flag = true;
                }
            }
            return flag;
        }
        #endregion



小注:

如果傳入的兩個值是dataRow中單元格的值,然後比較該類型,相一致拷貝部份程式碼:


C# 實體類別序列化與反序列化一(XmlSerializer)


C# 實體類化序列化與反共化物類序列化與反共化以上就是C#  物件比較(值類型、引用類型)的內容,更多相關內容請關注PHP中文網(www.php.cn)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# DataRow 比較下一篇:C# DataRow 比較