-
Notifications
You must be signed in to change notification settings - Fork 0
/
IEquatable.dib
142 lines (102 loc) · 3.3 KB
/
IEquatable.dib
1
2
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!markdown
# IEquatable
#!markdown
### Plain Class
#!csharp
public class ComplexNumber
{
public int Imaginary { get; set; }
public int Real { get; set; }
public ComplexNumber(int real, int imaginary)
{
Imaginary = imaginary;
Real = real;
}
public override string ToString()
{
return $"{Real} + {Imaginary}i";
}
}
#!csharp
display("");
var c1 = new ComplexNumber(1, 2);
var c2 = new ComplexNumber(1, 2);
Console.WriteLine($"c1 == c2: {c1.Equals(c2)}");
List<ComplexNumber> list = new List<ComplexNumber>();
list.Add(c1);
Console.WriteLine($"c1 in List: {list.Contains(c1)}");
Console.WriteLine($"c2 in List: {list.Contains(c2)}");
Console.WriteLine($"c2 in List: {list.IndexOf(c2)}");
#!markdown
### IEquatable implementation
#!csharp
public class ComplexNumberWithInterface: ComplexNumber, IEquatable<ComplexNumberWithInterface>
{
public ComplexNumberWithInterface(int real, int imaginary): base(real, imaginary) {}
public bool Equals(ComplexNumberWithInterface other)
{
return other.Real == this.Real && other.Imaginary == this.Imaginary;
}
}
#!csharp
display("");
var c1 = new ComplexNumberWithInterface(1, 2);
var c2 = new ComplexNumberWithInterface(1, 2);
Console.WriteLine($"c1 == c2: {c1.Equals(c2)}");
List<ComplexNumberWithInterface> list = new List<ComplexNumberWithInterface>();
list.Add(c1);
Console.WriteLine($"c1 in List: {list.Contains(c1)}");
Console.WriteLine($"c2 in List: {list.Contains(c2)}");
Console.WriteLine($"c2 in List: {list.IndexOf(c2)}");
Console.WriteLine($"c2 in List: {list.Contains(new ComplexNumberWithInterface(1, 2))}");
#!markdown
### Full Implementation
#!csharp
Console.WriteLine($"c1 == c2: {c1.Equals((object)c2)}");
Console.WriteLine($"c1 == c2: {c1 == c2}");
#!csharp
public class ComplexNumberFull: ComplexNumber, IEquatable<ComplexNumberFull>
{
public ComplexNumberFull(int real, int imaginary): base(real, imaginary) {}
public bool Equals(ComplexNumberFull other)
{
Console.WriteLine("Equals(ComplexNumberFull other)");
return other.Real == this.Real && other.Imaginary == this.Imaginary;
}
public override bool Equals(object? other)
{
Console.WriteLine("Equals(object? other)");
return other switch
{
ComplexNumberFull cn => this.Equals(cn),
_ => false
};
}
public override int GetHashCode()
{
return Real.GetHashCode() * 10 + Imaginary.GetHashCode();
}
public static bool operator ==(ComplexNumberFull cn1, ComplexNumberFull cn2)
{
Console.WriteLine("operator ==");
return cn1.Equals(cn2);
}
public static bool operator !=(ComplexNumberFull cn1, ComplexNumberFull cn2)
{
Console.WriteLine("operator !=");
return !cn1.Equals(cn2);
}
}
#!csharp
display("");
var c1 = new ComplexNumberFull(1, 2);
var c2 = new ComplexNumberFull(1, 2);
Console.WriteLine($"c1 == c2: {c1.Equals(c2)}");
List<ComplexNumberFull> list = new List<ComplexNumberFull>();
list.Add(new ComplexNumberFull(3, 4));
list.Add(c1);
Console.WriteLine($"c1 in List: {list.Contains(c1)}");
Console.WriteLine($"c2 in List: {list.Contains(c2)}");
Console.WriteLine($"c2 in List: {list.IndexOf(c2)}");
Console.WriteLine($"c1 == c2: {c1.Equals((object)c2)}");
Console.WriteLine($"c1 == c2: {c1 == c2}");