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个对象的值是否相等的更多相关文章

  1. JavaScript判断两个对象内容是否相等

    ES6中有一个方法判断两个对象是否相等,这个方法判断是两个对象引用地址是否一致 let obj1= { a: 1 } let obj2 = { a: 1 } console.log(Object.is ...

  2. Struts2 用 s:if test 判断String类型的对象属性值和单字符是否相等的问题

    Struts2 用 s:if test 判断String类型的对象属性值和单字符是否相等的问题   首先,这里所指的单字符形如:Y,男. 有两种做法: a. <s:if test='news.s ...

  3. 【freemaker】之循环,判断,对象取值

    entity: public class Employee { private Integer id; private String name; private Integer age; privat ...

  4. javascript判断两个对象属性以及值是否相等

    objIsEqual(obj1,obj2){//比较两个对象键值对是否相等 var o1 = obj1 instanceof Object; var o2 = obj2 instanceof Obje ...

  5. python中的is判断引用的对象是否一致,==判断值是否相等

    python中的is判断引用的对象是否一致,==判断值是否相等 a = 10 b = 20 list = [1,2,3,4,5] print(a in list) print(b not in lis ...

  6. js判断是否是对象获取子窗体值

    判断是否是对象 Object.prototype.toString.call(obj) 装换为数组 Array.prototype.slice.call(obj) 父窗体获取值子窗体值$(functi ...

  7. 【java】【反射】反射实现判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更

    java的反射实现: 判断发生了修改操作,判断两个对象是否发生属性值的变更,判断两个List集合内对象的属性值是否发生变更 今日份代码: package com.sxd.streamTest; imp ...

  8. js关于对象键值为数字型时输出的对象自动排序问题的解决方法

    一.对象键值为数字型时输出的对象自动排序问题如: var objs = {    "1603":{id:"1603"},    "1702" ...

  9. 更新AD对象属性值

    1. 对于Set-ADUser不包含的对象属性,可以采用replace来操作 Set-ADUser -Identity 'UserA' -Replace @{userWorkstations = 'C ...

随机推荐

  1. C++ static内容小结

    C++中static总结比较好的博客:http://blog.csdn.net/laixingjun/article/details/9139839 http://blog.csdn.net/xiaj ...

  2. nginx+tomcat反向代理下使用tomcat-redis-session-manager进行session共享中值得注意的一个问题

    公司目前项目使用nginx反向代理+多个tomcat进行负载均衡,之前使用ip_hash策略进行session控制.近期有考虑不再使用ip_hash策略,所以需要进行session共享. 根据项目实际 ...

  3. python中struct模块及packet和unpacket

    转自:http://www.cnblogs.com/gala/archive/2011/09/22/2184801.html 我们知道python只定义了6种数据类型,字符串,整数,浮点数,列表,元组 ...

  4. Tkinter教程之Event篇(2)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1823548 '''Tkinter教程之Event篇(2)''''''5.测试离开(Leave) ...

  5. [Hive - LanguageManual] Import/Export

    LanguageManual ImportExport     Skip to end of metadata   Added by Carl Steinbach, last edited by Le ...

  6. lucene学习笔记:三,Lucene的索引文件格式

    Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...

  7. 删除 Mac OS X 中“打开方式”里重复或无用的程序列表

    如果右键菜单的「打开方式」里出现了已不存在的应用程序或者重复的项目,打开终端,执行以下命令: /System/Library/Frameworks/CoreServices.framework/Ver ...

  8. 搭建Titanium开发环境

    轻松制作 App 再也不是梦! Titanium Mobile 让你能够使用你所熟悉的 web 技术,制作出如同使用Objective-C 或 Java 写出的 Native App. 除了有多达三百 ...

  9. HTTP响应报文与工作原理详解

    超文本传输协议(Hypertext Transfer Protocol,简称HTTP)是应用层协议.HTTP 是一种请求/响应式的协议,即一个客户端与服务器建立连接后,向服务器发送一个请求;服务器接到 ...

  10. 软件工程 --- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] [附加题]

    软件工程 --- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试] [附加题] 首先,在分组之前,我和室友薛亚杰已经详细阅读了往届学长的博客,认为电梯调度 ...