IEqualityComparer 接口使用

来源:互联网 发布:七氟醚的mac 编辑:程序博客网 时间:2024/06/15 23:58
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections; namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Console.WriteLine(10<<2);        }         private static void NewMethod1()        {            List<Persion> list = new List<Persion>            {                new Persion() {name="Jack",age=20,address="XX"},                new Persion() {name="Rose",age=21,address="X2"} ,                new Persion() {name="Rose",age=20,address="XX"} ,                new Persion() {name="Jack",age=20,address="XX"}            };             list = list.Distinct(new PersionCompare()).ToList();        }        public class PersionCompare : IEqualityComparer<Persion>        {            public bool Equals(Persion x, Persion y)            {                return x.name == y.name && x.age == y.age && x.address == y.address;            }             public int GetHashCode(Persion obj)            {                if (Object.ReferenceEquals(obj, null))                {                    return 0;                }                int name = obj.name == null ? 0 : obj.name.GetHashCode();                int age = obj.age.GetHashCode();                int address = obj.address == null ? 0 : obj.address.GetHashCode();                int hasCode = name ^ age ^ address;                Console.WriteLine(hasCode);                return hasCode;             }        }        private static Dictionary<string, string> GetSysFlag        {            get            {                Dictionary<string, string> dic = new Dictionary<string, string>();                string urlQuery = "".Replace("?", "");                foreach (string s in urlQuery.Split('&'))                {                    string[] urs = s.Split('=');                    if (urs.Length >= 2 && urs[0] != "") dic[urs[0]] = urs[1];                }                return dic;            }        }        private static void NewMethod()        {            string identityCard = "42062419881113292x";            string birthday = "";            string sex = "";            if (identityCard.Length == 18)//处理18位的身份证号码从号码中得到生日和性别代码            {                birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2);                sex = identityCard.Substring(14, 3);            }            if (identityCard.Length == 15)            {                birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2);                sex = identityCard.Substring(12, 3);            }             if (int.Parse(sex) % 2 == 0)//性别代码为偶数是女性奇数为男性            {                sex = "女";            }            else            {                sex = "男";            }            Console.WriteLine("出生" + birthday + "性别:" + sex);        }    }    public class Persion    {        public string name { get; set; }        public int age { get; set; }        public string address { get; set; }    }}

0 0