關於C# 比較的判斷

來源:互聯網
上載者:User

標籤:style   class   blog   code   color   com   

   private bool Compare(object o1, object o2)        {            if (o1 == null)            {                if (o2 == null)                {                    return true;                }                else                {                    return false;                }            }            else if (o1 != null && o2 != null)            {                return DataType(o1, o2);            }            else            {                if (o1 == null && o2 != null&&o2.ToString()=="")                {                    return true;                }                if (o2 == null && o1 != null && o1.ToString()=="")                {                    return true;                }                return o1.Equals(o2);            }        }        private bool DataType(object o1, object o2)        {            try            {                string type = o1.GetType().Name;                switch (type)                {                    case "Int32":                        return Convert.ToInt32(o1) == Convert.ToInt32(o2);                    case "Double":                        return Convert.ToDouble(o1) == Convert.ToDouble(o2);                    case "Decimal":                        return Convert.ToDecimal(o1) == Convert.ToDecimal(o2);                    case "Single":                        return Convert.ToSingle(o1) == Convert.ToSingle(o2);                    case "Char":                        return Convert.ToChar(o1) == Convert.ToChar(o2);                    case "String":                        return o1.ToString() == o2.ToString();                    case "Boolean":                        return Convert.ToBoolean(o1) == Convert.ToBoolean(o2);                    case "Int16":                        return Convert.ToInt16(o1) == Convert.ToInt16(o2);                    case "Int64":                        return Convert.ToInt64(o1) == Convert.ToInt64(o2);                    default:                        return o1 == o2;                }                        }            catch            {                return false;            }        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.