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 ...
随机推荐
- 一道JAVA经典面试题目的两种解法
题目要求:String s="-1 2 5 78 129 -65 -23";将字符串进行升序排序后输出. 方法一:使用数组进行排序 思路: 1.获取字符串中的数值: 2.将数组 ...
- Claim-based-security for ASP.NET Web APIs using DotNetOpenAuth
Recently I worked with a customer assisting them in implementing their Web APIs using the new ASP.NE ...
- oracle检查点checkpoint信息
1.关于checkpoint的概述 checkpoint是oracle在数据库一致性关闭.实例恢复和oracle基本操作中不可缺少的机制,包含以下相关的含义: A.检查点的位置(checkpoint ...
- 【跟我一起学python吧】python chr()、unichr()和ord()
chr().unichr()和ord() chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符.unichr()跟它一样,只不过返回的是Unicode字符 ...
- HDU 1890 Robotic Sort(splay)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...
- [HIve - LanguageManual] XPathUDF
Documentation for Built-In User-Defined Functions Related To XPath UDFs xpath, xpath_short, xpath_in ...
- 《Genesis-3D开源游戏引擎-官方录制系列视频教程:进阶实例篇》
注:本系列教程仅针对引擎编辑器:v1.2.2及以下版本 G3D进阶实例 第四课<2D编辑与脚本的统一入口> 使用G3D完成一个简单的类飞机大战游戏,介绍了G3D2d游戏制作的流程包括: ...
- java BigInteger类的用法
import java.math.BigInteger; Scanner in = new Scanner(System.in); BigInteger x1 = new BigInteger(&qu ...
- 使用SignalR实现比特币价格实时刷新
ASP.NET SignalR是微软支持的一个运行在 Dot NET 平台上的 HTML Websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重 ...
- Adress
流水账的写法:因为不想让亲爱的你只看开头就关掉了我辛苦的劳作.流水账的好处是:便于逻辑的理解 http://software.intel.com/zh-cn/blogs/2014/01/20/cent ...