Skip to content

Commit f91a551

Browse files
author
ahotko
committed
Implemented IEquality<T> and IEqualityComparer<T> interfaces
1 parent bcb3b45 commit f91a551

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

CSharp Code Samples/CodeSamples/CodeSamples.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
<Compile Include="Classes\ConstructorChainingSample.cs" />
6060
<Compile Include="Attributes\DebuggingSample.cs" />
6161
<Compile Include="Classes\ExtensionMethodsSample.cs" />
62+
<Compile Include="Comparing\CompareSample.cs" />
63+
<Compile Include="Comparing\EqualityComparer.cs" />
64+
<Compile Include="Comparing\Equatable.cs" />
6265
<Compile Include="Constants.cs" />
6366
<Compile Include="ISampleExecute.cs" />
6467
<Compile Include="MultiThreading\BackgroundWorkerSample.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
5+
namespace CodeSamples.Comparing
6+
{
7+
public class CompareSample : SampleExecute
8+
{
9+
private void EquatableAddToList(Collection<EquatableSample> list, EquatableSample item)
10+
{
11+
if (!list.Contains(item))
12+
{
13+
list.Add(item);
14+
Console.WriteLine($"Added Sample {item.ToString()}");
15+
}
16+
else
17+
{
18+
Console.WriteLine($"Sample '{item.ToString()}' already in list; not added");
19+
}
20+
}
21+
22+
private void EquatableSample()
23+
{
24+
var list = new Collection<EquatableSample>();
25+
26+
var item1 = new EquatableSample() { IntProperty = 1, StringProperty = "One" };
27+
var item2 = new EquatableSample() { IntProperty = 1, StringProperty = "Two" };
28+
var item3 = new EquatableSample() { IntProperty = 3, StringProperty = "Three" };
29+
var item4 = new EquatableSample() { IntProperty = 1, StringProperty = "One" };
30+
31+
EquatableAddToList(list, item1);
32+
EquatableAddToList(list, item2);
33+
EquatableAddToList(list, item3);
34+
EquatableAddToList(list, item4);
35+
}
36+
37+
private void EqualityComparerAddToList(Dictionary<EqualityComparerSample, string> list, EqualityComparerSample item)
38+
{
39+
try
40+
{
41+
list.Add(item, item.StringProperty);
42+
Console.WriteLine($"Added Sample {item.ToString()}");
43+
}
44+
catch (ArgumentException e)
45+
{
46+
Console.WriteLine($"Sample '{item.ToString()}' already in list; not added");
47+
}
48+
}
49+
50+
private void EqualityComparerSample()
51+
{
52+
//IEqualityComparer<T> is only for dictionaries
53+
var list = new Dictionary<EqualityComparerSample, string>(new EqualityComparer());
54+
55+
var item1 = new EqualityComparerSample() { IntProperty = 1, StringProperty = "One" };
56+
var item2 = new EqualityComparerSample() { IntProperty = 1, StringProperty = "Two" };
57+
var item3 = new EqualityComparerSample() { IntProperty = 3, StringProperty = "Three" };
58+
var item4 = new EqualityComparerSample() { IntProperty = 1, StringProperty = "One" };
59+
60+
EqualityComparerAddToList(list, item1);
61+
EqualityComparerAddToList(list, item2);
62+
EqualityComparerAddToList(list, item3);
63+
EqualityComparerAddToList(list, item4);
64+
}
65+
66+
public override void Execute()
67+
{
68+
Title("CompareSampleExecute");
69+
LineBreak();
70+
Section("IEquatable<T> Sample");
71+
EquatableSample();
72+
LineBreak();
73+
Section("IEqualityComparer<T> Sample");
74+
EqualityComparerSample();
75+
LineBreak();
76+
Finish();
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
3+
namespace CodeSamples.Comparing
4+
{
5+
public class EqualityComparer : IEqualityComparer<EqualityComparerSample>
6+
{
7+
public bool Equals(EqualityComparerSample x, EqualityComparerSample y)
8+
{
9+
if (x is null && y is null) return true;
10+
if (x is null || y is null) return false;
11+
return (x.IntProperty == y.IntProperty && x.StringProperty == y.StringProperty);
12+
}
13+
14+
public int GetHashCode(EqualityComparerSample obj)
15+
{
16+
return obj.IntProperty.GetHashCode() ^ obj.StringProperty.GetHashCode();
17+
}
18+
}
19+
20+
public class EqualityComparerSample
21+
{
22+
public int IntProperty { get; set; }
23+
public string StringProperty { get; set; }
24+
25+
public override string ToString()
26+
{
27+
return $"Int = {IntProperty}, String = {StringProperty}";
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace CodeSamples.Comparing
4+
{
5+
public sealed class EquatableSample : IEquatable<EquatableSample>
6+
{
7+
public int IntProperty { get; set; }
8+
public string StringProperty { get; set; }
9+
10+
public bool Equals(EquatableSample other)
11+
{
12+
if (other is null) return false;
13+
return (other.IntProperty == IntProperty && other.StringProperty == StringProperty);
14+
}
15+
16+
public override string ToString()
17+
{
18+
return $"Int = {IntProperty}, String = {StringProperty}";
19+
}
20+
}
21+
}

CSharp Code Samples/CodeSamples/Program.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CodeSamples.Alterations;
22
using CodeSamples.Attributes;
33
using CodeSamples.Classes;
4+
using CodeSamples.Comparing;
45
using CodeSamples.MultiThreading;
56
using CodeSamples.Patterns;
67
using CodeSamples.SyntacticSugars;
@@ -107,6 +108,11 @@ static void Main(string[] args)
107108
backgroundWorkerSample.Execute();
108109
#endregion
109110

111+
#region Equality
112+
var compareSample = new CompareSample();
113+
compareSample.Execute();
114+
#endregion
115+
110116
Console.WriteLine();
111117
Console.WriteLine("End Code Samples");
112118

0 commit comments

Comments
 (0)