IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> c#比较两个object的值是否一样 -> 正文阅读

[开发工具]c#比较两个object的值是否一样

#region //两个值是否相等
        /// <summary>
        /// 比较两个object对象值是否相等,用Equal和RefrenceEqual或者==都不能直接比较
        /// 可以用static class扩展object类型
        /// </summary>
        /// <param name="a">数值a</param>
        /// <param name="b">数值b</param>
        /// <param name="类型">S,N,D,L,O几个类型</param>
        /// <returns></returns>
        public static bool 两个值是否相等(object a, object b, string 类型 = "")
        {
            bool result = false, l1 = false, l2 = false;
            if ((l1 = (a == null || a == DBNull.Value)) && (l2 = (b == null || b == DBNull.Value))) return true;  //两个都是null
            if (l1 != l2) return false;     //一个是null一个不是null
            //根据传入的类型判断
            string sType = 类型.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);
            //是否数字
            if (GsDefineTypes.isNumber(a.GetType()) && GsDefineTypes.isNumber(b.GetType())) return Convert.ToDouble(a) == Convert.ToDouble(b);
            return object.Equals(a, b);
        }
        #endregion


        #region //getDefaultValueOfType:取类型的默认值
        /// <summary>
        /// 取类型的默认值,比default(T)好在便于控制
        /// </summary>
        /// <param name="type">要取的类型</param>
        /// <returns>返回默认值,object类型返回DBNull</returns>
        public static object getDefaultValueOfType(Type 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 = "";
            //其他类型
            else if (type == typeof(System.DateTime) || type == typeof(System.DateTime?)) result = DateTime.Today;
            else if (type == typeof(System.Guid)) result = Guid.NewGuid();
            //全部都不是,只是基类
            else if (type == typeof(System.Object)) result = null;
            //
            return result;
        }
        #endregion


        #region //是数字类型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

直接用==、equal、refrenceEqual判断不大行。

比如一个int和一个double都是0,直接object.equal返回是不相等,因为一个是int一个double。

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-05-12 16:36:29  更:2022-05-12 16:36:46 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 3:16:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码