C#_判斷2個對象的值是否相等

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   

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();        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.