zoukankan      html  css  js  c++  java
  • 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 = 31;
            int result = 1;
            result = prime * result + ((list == null) ? 0 : list.GetHashCode());
            result = prime * result + age;
            result = prime * result + ((name == null) ? 0 : 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(0) && other.list.Except(list).Count().Equals(0)))
                    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 = 20;
                p1.list.Add(2);
                p1.list.Add(1);
               
         
                p2.name = "aaa";
                p2.age = 20;
                p2.list.Add(1);
                p2.list.Add(2);
              
                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();
    
            }
        }
    }
  • 相关阅读:
    k8s 节点的 NodeAffinity 使用
    template 与 host , item trigger的关系
    mysql 性能优化思路
    nginx 配sorry page
    修改tomcat JVM 大小 jdk--目录修改
    (转)MySQL慢查询分析优化 + MySQL调优
    注册表操作 Microsoft.Win32.Registry与RegistryKey类
    C#(99):WCF之.NET Remoting通讯
    CallContext线程数据缓存-调用上下文
    C#(99):JSON与对象的序列化与反序列化
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3857521.html
Copyright © 2011-2022 走看看