C#_判断2个对象的值是否相等
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; namespace ConsoleApplication1
{ class Person
{ public Person()
{
list = new HashSet<int>();
}
public int age { set; get; }
public string name { set; get; } public ICollection<int> list { set; get; } public override int GetHashCode()
{
int prime = ;
int result = ;
result = prime * result + ((list == null) ? : list.GetHashCode());
result = prime * result + age;
result = prime * result + ((name == null) ? : name.GetHashCode());
return result;
} public override bool Equals(object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (this.GetType() != obj.GetType())
return false;
Person other = (Person)obj;
if (list == null)
{
if (other.list != null)
return false;
}
//else if (!list.Equals(other.list))
// return false; else if (!(list.Except(other.list).Count().Equals() && other.list.Except(list).Count().Equals()))
return false;
if (age != other.age)
return false;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.Equals(other.name))
return false;
return true;
}
}
class Program
{
static void Main(string[] args)
{ //HashSet<int> s1 = new HashSet<int>();
//HashSet<int> s2 = new HashSet<int>();
//s1.Add(1);
//s1.Add(2);
//s1.Add(3);
//s2.Add(1);
//s2.Add(2); //var list3 = s2.Except(s1).ToList(); //Console.WriteLine(list3.Count().Equals(0)); //foreach (var i in list3)
//{
//Console.WriteLine(i.ToString());
//}
Person p1 = new Person(); Person p2 = new Person(); p1.name = "aaa";
p1.age = ;
p1.list.Add();
p1.list.Add(); p2.name = "aaa";
p2.age = ;
p2.list.Add();
p2.list.Add(); Console.WriteLine(p1.Equals(p2)); //var t = p1.GetType();
//var p1Model = Activator.CreateInstance(t); //var pros = t.GetProperties(); //int total = pros.Count();
//Console.WriteLine("total: "+total);
//var t2 = p2.GetType();
//var p2Model = Activator.CreateInstance(t2);
//var pros2 = t2.GetProperties(); //bool flag = false; //for (int i = 0; i < total; i++)
//{
// Console.WriteLine(pros[i].GetValue(p1, null) + "---" + pros2[i].GetValue(p2, null));
// if (pros[i].GetValue(p1,null).ToString() != pros2[i].GetValue(p2,null).ToString())
// {
// Console.WriteLine("In if"+pros[i].GetValue(p1, null) + "---" + pros2[i].GetValue(p2, null));
// flag = true;
// }
//}
//Console.WriteLine(flag); //Person p1 = new Person(); //Person p2 = new Person(); //p1.name = "aaa";
//p1.age = 21; //p2.name = "aaa";
//p2.age = 21; //Console.WriteLine(p1.Equals(p2)); //var p1Type = p1.GetType();
//var p2Type = p2.GetType(); //var p1Properties = p1Type.GetProperties();
//var p2Properties = p2Type.GetProperties(); //var type = typeof(Person); //var result = p1Properties.All(p => p2Properties.Where(q => q.Name == p.Name).FirstOrDefault().GetValue(p2).Equals(p.GetValue(p1)));
//Console.WriteLine(result);
Console.ReadKey(); }
}
}
C#_判断2个对象的值是否相等的更多相关文章
- JavaScript判断两个对象内容是否相等
ES6中有一个方法判断两个对象是否相等,这个方法判断是两个对象引用地址是否一致 let obj1= { a: 1 } let obj2 = { a: 1 } console.log(Object.is ...
- Struts2 用 s:if test 判断String类型的对象属性值和单字符是否相等的问题
Struts2 用 s:if test 判断String类型的对象属性值和单字符是否相等的问题 首先,这里所指的单字符形如:Y,男. 有两种做法: a. <s:if test='news.s ...
- 【freemaker】之循环,判断,对象取值
entity: public class Employee { private Integer id; private String name; private Integer age; privat ...
- javascript判断两个对象属性以及值是否相等
objIsEqual(obj1,obj2){//比较两个对象键值对是否相等 var o1 = obj1 instanceof Object; var o2 = obj2 instanceof Obje ...
- python中的is判断引用的对象是否一致,==判断值是否相等
python中的is判断引用的对象是否一致,==判断值是否相等 a = 10 b = 20 list = [1,2,3,4,5] print(a in list) print(b not in lis ...
- js判断是否是对象获取子窗体值
判断是否是对象 Object.prototype.toString.call(obj) 装换为数组 Array.prototype.slice.call(obj) 父窗体获取值子窗体值$(functi ...
- 【java】【反射】反射实现判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更
java的反射实现: 判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更 今日份代码: package com.sxd.streamTest; imp ...
- js关于对象键值为数字型时输出的对象自动排序问题的解决方法
一.对象键值为数字型时输出的对象自动排序问题如: var objs = { "1603":{id:"1603"}, "1702" ...
- 更新AD对象属性值
1. 对于Set-ADUser不包含的对象属性,可以采用replace来操作 Set-ADUser -Identity 'UserA' -Replace @{userWorkstations = 'C ...
随机推荐
- 【windows核心编程】IO完成端口(IOCP)复制文件小例前简单说明
1.关于IOCP IOCP即IO完成端口,是一种高伸缩高效率的异步IO方式,一个设备或文件与一个IO完成端口相关联,当文件或设备的异步IO操作完成的时候,去IO完成端口的[完成队列]取一项,根据完成键 ...
- SqlHelper 带详细中文注释
using System; using System.Collections.Generic; using System.Linq; using System.Text; //对数据库进行操作引入命名 ...
- SQL中以count及sum为条件的查询
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
- 在阿里云linux下使用SVN访问VisualSVN出错:SSL handshake failed: SSL error: Key usage violation in certificate has been detected
Subversion clients receive the following error message when attempting to connect to VisualSVN Serve ...
- NodeJs 开源
iwebpp.io - 运行P2P Node.js web 服务,穿透防火墙,NAT https://github.com/InstantWebP2P/iwebpp.io pm 是一个轻量级的Node ...
- Spark RDD概念学习系列之Spark的算子的作用(十四)
Spark的算子的作用 首先,关于spark算子的分类,详细见 http://www.cnblogs.com/zlslch/p/5723857.html 1.Transformation 变换/转换算 ...
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- C#学习笔记(十六):Attribute
Attribute可以为类或方法添加一些附加的信息,我们可以看看MSDN对Attribute的描述: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注 ...
- windbg命令分类与概述
WinDBG的大多数功能是以命令方式工作的, 本系列将介绍WinDBG的三类命令, 标准命令, 元命令和扩展命令. =============== 标准命令 =============== 标准命令用 ...
- 常见MFC UI界面库
Xtrme toolkit,BCGControlBar,SkinMagic,AppFace,Skin++,Uskin++,SYGUI,LibUIDK,GuiToolkit,GardenUI等等,除了后 ...