| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 
 | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            var objA = new Toto { A = 45, B = "X" };
            var objB = new Toto { A = 4, B = "D" };
            var objC = new Toto { A = 5, B = "X" };
            var objD = new Toto { A = 45, B = "A" };
 
            WriteToto(objA);
            WriteToto(objB);
            WriteToto(objC);
            WriteToto(objD);
 
            //Ne doit retourner que des TRUE
            Console.WriteLine(objA.GetHashCode() == objB.GetHashCode());
            Console.WriteLine(objA.GetHashCode() != objC.GetHashCode());
            Console.WriteLine(objA.GetHashCode() == objC.GetHashCode());
            Console.WriteLine(objA.GetHashCode() == objD.GetHashCode());
 
            var listA = new List<Toto>();
            var listB = new List<Toto>();
 
            listA.Add(objA);
            listA.Add(objB);
            listA.Add(objC);
            listA.Add(objD);
 
            listB.Add(objA);
 
            var list = listA.Intersect(listB, new TotoComparer()).ToList();
            //Normalement je devrai récuperer A, C, D
            Console.WriteLine(list.Count == 3);
            Console.WriteLine(list.Contains(objA));
            Console.WriteLine(list.Contains(objC));
            Console.WriteLine(list.Contains(objD));
 
            list = listA.Except(listB, new TotoComparer()).ToList();
            //Normalement je devrai récuperer B
            Console.WriteLine(list.Count == 1);
            Console.WriteLine(list.Contains(objB));
            Console.ReadLine();
        }
 
        public static void WriteToto(Toto t)
        {
            Console.WriteLine(t.ToString() + " " + t.GetHashCode());
        }
    }
 
    public class Toto
    {
        public long? A { get; set; }
        public string B { get; set; }
 
        public string C { get; set; }
 
        public override string ToString()
        {
            return base.ToString();
        }
 
        public override int GetHashCode()
        {
            //hash code à faire
            return A.GetHashCode();
        }
    }
 
    public class TotoComparer : IEqualityComparer<Toto>
    {
        public bool Equals(Toto x, Toto y)
        {
            return
                (
                    (x.A.HasValue) && (x.A == y.A)
                 ||
                    (x.B == y.B)
                    )
 
 
                ;
        }
 
        public int GetHashCode(Toto obj)
        {
            return obj.GetHashCode();
        }
    }
} | 
Partager