Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7920fa7

Browse files
authored
IEquatable<T> F# snippets (#7767)
1 parent d6a9a78 commit 7920fa7

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
module EqualsEx2
2+
3+
// <Snippet3>
4+
open System
5+
open System.Text.RegularExpressions
6+
7+
type Person(lastName, ssn) =
8+
let mutable lastName = lastName
9+
let ssn =
10+
if Regex.IsMatch(ssn, @"\d{9}") then
11+
$"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}"
12+
elif Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}") then
13+
ssn
14+
else
15+
raise (FormatException "The social security number has an invalid format.")
16+
17+
member _.SSN =
18+
ssn
19+
20+
member _.LastName
21+
with get () = lastName
22+
and set (value) =
23+
if String.IsNullOrEmpty value then
24+
invalidArg (nameof value) "The last name cannot be null or empty."
25+
else
26+
lastName <- value
27+
28+
static member op_Equality (person1: Person, person2: Person) =
29+
if box person1 |> isNull || box person2 |> isNull then
30+
Object.Equals(person1, person2)
31+
else
32+
person1.Equals person2
33+
34+
static member op_Inequality (person1: Person, person2: Person) =
35+
if box person1 |> isNull || box person2 |> isNull then
36+
Object.Equals(person1, person2) |> not
37+
else
38+
person1.Equals person2 |> not
39+
40+
override _.GetHashCode() =
41+
ssn.GetHashCode()
42+
43+
override this.Equals(obj: obj) =
44+
match obj with
45+
| :? Person as personObj ->
46+
(this :> IEquatable<_>).Equals personObj
47+
| _ -> false
48+
49+
interface IEquatable<Person> with
50+
member this.Equals(other: Person) =
51+
match box other with
52+
| null -> false
53+
| _ ->
54+
this.SSN = other.SSN
55+
// </Snippet3>
56+
// Create a Person object for each job applicant.
57+
let applicant1 = Person("Jones", "099-29-4999")
58+
let applicant2 = Person("Jones", "199-29-3999")
59+
let applicant3 = Person("Jones", "299-49-6999")
60+
61+
// Add applicants to a List object.
62+
let applicants = ResizeArray()
63+
applicants.Add applicant1
64+
applicants.Add applicant2
65+
applicants.Add applicant3
66+
67+
// Create a Person object for the final candidate.
68+
let candidate = Person("Jones", "199-29-3999")
69+
if applicants.Contains candidate then
70+
printfn $"Found {candidate.LastName} (SSN {candidate.SSN})."
71+
else
72+
printfn $"Applicant {candidate.SSN} not found."
73+
74+
// Call the shared inherited Equals(Object, Object) method.
75+
// It will in turn call the IEquatable<T>.Equals implementation.
76+
printfn $"{applicant2.LastName}({applicant2.SSN}) already on file: {Person.Equals(applicant2, candidate)}."
77+
78+
// The example displays the following output:
79+
// Found Jones (SSN 199-29-3999).
80+
// Jones(199-29-3999) already on file: True.
81+
82+
// This tests the handling of null values, but does not appear in the documentation.
83+
//
84+
//public class Example
85+
//{
86+
// public static void Main()
87+
// {
88+
// var p1 = new Person("Joe", "613-24-0068")
89+
// Person p2 = null
90+
//
91+
// Console.WriteLine(p1 == p2)
92+
// }
93+
//}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
open System
2+
open System.Collections.Generic
3+
open System.Text.RegularExpressions
4+
5+
type Person(lastName, ssn) =
6+
let mutable lastName = lastName
7+
let ssn =
8+
if Regex.IsMatch(ssn, @"\d{9}") then
9+
$"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}"
10+
elif Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}") then
11+
ssn
12+
else
13+
raise (FormatException "The social security number has an invalid format.")
14+
15+
member _.SSN =
16+
ssn
17+
18+
member _.LastName
19+
with get () = lastName
20+
and set (value) =
21+
if String.IsNullOrEmpty value then
22+
invalidArg (nameof value) "The last name cannot be null or empty."
23+
else
24+
lastName <- value
25+
26+
static member op_Equality (person1: Person, person2: Person) =
27+
if box person1 |> isNull || box person2 |> isNull then
28+
Object.Equals(person1, person2)
29+
else
30+
person1.Equals person2
31+
32+
static member op_Inequality (person1: Person, person2: Person) =
33+
if box person1 |> isNull || box person2 |> isNull then
34+
Object.Equals(person1, person2) |> not
35+
else
36+
person1.Equals person2 |> not
37+
38+
override _.GetHashCode() =
39+
ssn.GetHashCode()
40+
41+
override this.Equals(obj: obj) =
42+
match obj with
43+
| :? Person as personObj ->
44+
(this :> IEquatable<_>).Equals personObj
45+
| _ -> false
46+
47+
interface IEquatable<Person> with
48+
member this.Equals(other: Person) =
49+
match box other with
50+
| null -> false
51+
| _ ->
52+
this.SSN = other.SSN
53+
54+
// <Snippet12>
55+
// Create a Person object for each job applicant.
56+
let applicant1 = Person("Jones", "099-29-4999")
57+
let applicant2 = Person("Jones", "199-29-3999")
58+
let applicant3 = Person("Jones", "299-49-6999")
59+
60+
// Add applicants to a List object.
61+
let applicants = ResizeArray()
62+
applicants.Add applicant1
63+
applicants.Add applicant2
64+
applicants.Add applicant3
65+
66+
// Create a Person object for the final candidate.
67+
let candidate = Person("Jones", "199-29-3999")
68+
if applicants.Contains candidate then
69+
printfn $"Found {candidate.LastName} (SSN {candidate.SSN})."
70+
else
71+
printfn $"Applicant {candidate.SSN} not found."
72+
73+
// Call the shared inherited Equals(Object, Object) method.
74+
// It will in turn call the IEquatable<T>.Equals implementation.
75+
printfn $"{applicant2.LastName}({applicant2.SSN}) already on file: {Person.Equals(applicant2, candidate)}."
76+
77+
// The example displays the following output:
78+
// Found Jones (SSN 199-29-3999).
79+
// Jones(199-29-3999) already on file: True.
80+
// </Snippet12>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="EqualsEx2.fs" />
9+
<Compile Include="Snippet12.fs" />
10+
</ItemGroup>
11+
</Project>

xml/System/IEquatable`1.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@
145145
The following example shows the partial implementation of a `Person` class that implements <xref:System.IEquatable%601> and has two properties, `LastName` and `SSN`. The <xref:System.IEquatable%601.Equals%2A> method returns `True` if the `SSN` property of two `Person` objects is identical; otherwise, it returns `False`.
146146
147147
:::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs" id="Snippet3":::
148+
:::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs" id="Snippet3":::
148149
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb" id="Snippet3":::
149150
150151
`Person` objects can then be stored in a <xref:System.Collections.Generic.List%601> object and can be identified by the `Contains` method, as the following example shows.
151152
152153
:::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs" id="Snippet12":::
154+
:::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs" id="Snippet12":::
153155
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb" id="Snippet12":::
154156
155157
]]></format>

0 commit comments

Comments
 (0)