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 ...
随机推荐
- 再谈Jquery Ajax方法传递到action(转)
之前写过一篇文章Jquery Ajax方法传值到action,本文是对该文的补充. 假设 controller中的方法是如下: public ActionResult ReadPerson(Perso ...
- [Hive - LanguageManual] Sampling
Sampling Syntax Sampling Bucketized Table Block Sampling Sampling Syntax 抽样语法 Sampling Bucketized T ...
- oracle 10g
一.安装系统 首先安装Linux系统,根据Oracle官方文档的建议,在机器内存小于1G的情况下,swap分区大小应该设置为内存的2倍大,若内存大于2G则swap分区设置为与内存大小一样. 为防止Or ...
- Spring EL hello world example
The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...
- TTL电平、CMOS电平、RS232电平的区别
工作中遇到一个关于电平选择的问题,居然给忘记RS232电平的定义了,当时无法反应上来,回来之后查找资料才了解两者之间的区别,视乎两年多的时间,之前非常熟悉的一些常识也开始淡忘,这个可不是一个好的现象. ...
- [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画
A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮 code s ...
- Angularjs 初始化框架
bindJQuery(); publishExternalAPI(angular); jqLite(document).ready(function() { angularInit(document, ...
- ThinkPHP3.1.3的单字母函数汇总
A函数: 用于实例化Action 格式:[项目://][分组/]模块 /** * A函数用于实例化Action 格式:[项目://][分组/]模块 * @param string $name Acti ...
- HR(人事管理)
HRMS(Human Resource Management System) --人员 per_people_f --人员分配 per_all_assignments_f --要素 DECLARE l ...
- Java中反射的三种常用方式
Java中反射的三种常用方式 package com.xiaohao.test; public class Test{ public static void main(String[] args) t ...