IEqualityComparer 接口使用
2015-11-12 14:21
330 查看
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; } } }
相关文章推荐
- easyui combobox
- 小米wifi
- Android 自定义长按事件
- 10.错误和异常
- [转载] 百度上传&下载脚本
- 基于zepto移动4*3九宫格转奖
- 浏览器的那些故事
- 微软当年的浏览器之战
- BIOS设置之UEFI/Legacy BIOS切换图文详解
- Java异常处理之try...catch...语句的使用进阶
- 约瑟夫问题
- onMeasure,onLayout,onDraw
- IOS四种保存文件的方式
- HTTPS、SPDY和HTTP/2的性能比较
- SqlServer中生成1~23之间的连续数字
- NET MVC中 DropDownList的BUG
- mongo创建用户
- php *-devel
- iOS中tableView的三种刷新方法
- Android 手势基础 GestureDetector和SimpleOnGestureListener的使用教程