c#---HashSet对类的判重

来源:互联网 发布:赤月传说2进阶数据 编辑:程序博客网 时间:2024/06/06 00:16
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace rand{    class Program    {        class E        {            public int a;            public int b;            public E(int aa, int bb)            {                a = aa;                b = bb;            }            public override bool Equals(object obj)            {                E e = obj as E;                return this.a == e.a && this.b == e.b;            }            public override int GetHashCode()            {                return a * 100 + b;            }        }        static void Main(string[] args)        {            HashSet<E> l = new HashSet<E>();            E A = new E(1, 2);            E B = new E(1, 2);            l.Add (A);            l.Add (B);            Console.WriteLine(l.Count);            Console.ReadLine();        }    }}

这里写图片描述

0 0
原创粉丝点击