Skip to content

Commit eae4150

Browse files
committed
initial attempt
1 parent 17a1a93 commit eae4150

File tree

7 files changed

+165
-384
lines changed

7 files changed

+165
-384
lines changed

snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,41 @@
11
// <Snippet3>
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Text.RegularExpressions;
5-
62
public class Person : IEquatable<Person>
73
{
8-
private string uniqueSsn;
9-
private string lName;
10-
11-
public Person(string lastName, string ssn)
12-
{
13-
if (Regex.IsMatch(ssn, @"\d{9}"))
14-
uniqueSsn = $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}";
15-
else if (Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}"))
16-
uniqueSsn = ssn;
17-
else
18-
throw new FormatException("The social security number has an invalid format.");
19-
20-
this.LastName = lastName;
21-
}
22-
23-
public string SSN
24-
{
25-
get { return this.uniqueSsn; }
26-
}
27-
28-
public string LastName
29-
{
30-
get { return this.lName; }
31-
set {
32-
if (String.IsNullOrEmpty(value))
33-
throw new ArgumentException("The last name cannot be null or empty.");
34-
else
35-
this.lName = value;
36-
}
37-
}
4+
public Person(string lastName, string ssn)
5+
{
6+
LastName = lastName;
7+
SSN = ssn;
8+
}
389

39-
public bool Equals(Person other)
40-
{
41-
if (other == null)
42-
return false;
10+
public string LastName { get; }
4311

44-
if (this.uniqueSsn == other.uniqueSsn)
45-
return true;
46-
else
47-
return false;
48-
}
12+
public string SSN { get; }
4913

50-
public override bool Equals(Object obj)
51-
{
52-
if (obj == null)
53-
return false;
14+
public bool Equals(Person? other) => other is not null && other.SSN == SSN;
5415

55-
Person personObj = obj as Person;
56-
if (personObj == null)
57-
return false;
58-
else
59-
return Equals(personObj);
60-
}
16+
public override bool Equals(object? obj) => Equals(obj as Person);
6117

62-
public override int GetHashCode()
63-
{
64-
return this.SSN.GetHashCode();
65-
}
18+
public override int GetHashCode() => SSN.GetHashCode();
6619

67-
public static bool operator == (Person person1, Person person2)
68-
{
69-
if (((object)person1) == null || ((object)person2) == null)
70-
return Object.Equals(person1, person2);
20+
public static bool operator ==(Person person1, Person person2)
21+
{
22+
if (person1 is null)
23+
{
24+
return person2 is null;
25+
}
7126

72-
return person1.Equals(person2);
73-
}
27+
return person1.Equals(person2);
28+
}
7429

75-
public static bool operator != (Person person1, Person person2)
76-
{
77-
if (((object)person1) == null || ((object)person2) == null)
78-
return ! Object.Equals(person1, person2);
30+
public static bool operator !=(Person person1, Person person2)
31+
{
32+
if (person1 is null)
33+
{
34+
return person2 is not null;
35+
}
7936

80-
return ! (person1.Equals(person2));
81-
}
37+
return !person1.Equals(person2);
38+
}
8239
}
8340
// </Snippet3>
8441

snippets/csharp/System/IEquatableT/Equals/Snippet12.cs

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,41 @@
44

55
public class Person : IEquatable<Person>
66
{
7-
private string uniqueSsn;
8-
private string lName;
7+
public Person(string lastName, string ssn)
8+
{
9+
LastName = lastName;
10+
SSN = ssn;
11+
}
912

10-
public Person(string lastName, string ssn)
11-
{
12-
this.SSN = ssn;
13-
this.LastName = lastName;
14-
}
13+
public string LastName { get; }
1514

16-
public string SSN
17-
{
18-
get { return this.uniqueSsn; }
19-
set {
20-
if (Regex.IsMatch(value, @"\d{9}"))
21-
uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3),
22-
value.Substring(3, 2),
23-
value.Substring(5, 4));
24-
else if (Regex.IsMatch(value, @"\d{3}-\d{2}-\d{4}"))
25-
uniqueSsn = value;
26-
else
27-
throw new FormatException("The social security number has an invalid format.");
28-
}
29-
}
15+
public string SSN { get; }
3016

31-
public string LastName
32-
{
33-
get { return this.lName; }
34-
set {
35-
if (String.IsNullOrEmpty(value))
36-
throw new ArgumentException("The last name cannot be null or empty.");
37-
else
38-
this.lName = value;
39-
}
40-
}
17+
public bool Equals(Person? other) => other is not null && other.SSN == SSN;
4118

42-
public bool Equals(Person other)
43-
{
44-
if (other == null)
45-
return false;
19+
public override bool Equals(object? obj) => Equals(obj as Person);
4620

47-
if (this.uniqueSsn == other.uniqueSsn)
48-
return true;
49-
else
50-
return false;
51-
}
21+
public override int GetHashCode() => SSN.GetHashCode();
5222

53-
public override bool Equals(Object obj)
54-
{
55-
if (obj == null)
56-
return false;
23+
public static bool operator ==(Person person1, Person person2)
24+
{
25+
if (person1 is null)
26+
{
27+
return person2 is null;
28+
}
5729

58-
Person personObj = obj as Person;
59-
if (personObj == null)
60-
return false;
61-
else
62-
return Equals(personObj);
63-
}
64-
65-
public override int GetHashCode()
66-
{
67-
return this.SSN.GetHashCode();
68-
}
69-
70-
public static bool operator == (Person person1, Person person2)
71-
{
72-
if (((object)person1) == null || ((object)person2) == null)
73-
return Object.Equals(person1, person2);
30+
return person1.Equals(person2);
31+
}
7432

75-
return person1.Equals(person2);
76-
}
33+
public static bool operator !=(Person person1, Person person2)
34+
{
35+
if (person1 is null)
36+
{
37+
return person2 is not null;
38+
}
7739

78-
public static bool operator != (Person person1, Person person2)
79-
{
80-
if (((object)person1) == null || ((object)person2) == null)
81-
return ! Object.Equals(person1, person2);
82-
83-
return ! (person1.Equals(person2));
84-
}
40+
return !person1.Equals(person2);
41+
}
8542
}
8643

8744
// <Snippet12>

snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,28 @@
22

33
// <Snippet3>
44
open System
5-
open System.Text.RegularExpressions
65

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.")
6+
type Person(lastName: string, ssn: string) =
7+
member this.LastName = lastName
8+
member this.SSN = ssn
169

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()
10+
interface IEquatable<Person> with
11+
member this.Equals(other: Person) =
12+
other.SSN = this.SSN
4213

4314
override this.Equals(obj: obj) =
44-
match obj with
45-
| :? Person as personObj ->
46-
(this :> IEquatable<_>).Equals personObj
15+
match obj with
16+
| :? Person as person -> (this :> IEquatable<Person>).Equals(person)
4717
| _ -> false
4818

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
19+
override this.GetHashCode() =
20+
this.SSN.GetHashCode()
21+
22+
static member (==) (person1: Person, person2: Person) =
23+
person1.Equals(person2)
24+
25+
static member (!=) (person1: Person, person2: Person) =
26+
not (person1.Equals(person2))
5527
// </Snippet3>
5628
// Create a Person object for each job applicant.
5729
let applicant1 = Person("Jones", "099-29-4999")

snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,29 @@
22
open System.Collections.Generic
33
open System.Text.RegularExpressions
44

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.")
5+
open System
146

15-
member _.SSN =
16-
ssn
7+
type Person(lastName: string, ssn: string) =
8+
member this.LastName = lastName
9+
member this.SSN = ssn
1710

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()
11+
interface IEquatable<Person> with
12+
member this.Equals(other: Person) =
13+
other.SSN = this.SSN
4014

4115
override this.Equals(obj: obj) =
42-
match obj with
43-
| :? Person as personObj ->
44-
(this :> IEquatable<_>).Equals personObj
16+
match obj with
17+
| :? Person as person -> (this :> IEquatable<Person>).Equals(person)
4518
| _ -> false
4619

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
20+
override this.GetHashCode() =
21+
this.SSN.GetHashCode()
22+
23+
static member (==) (person1: Person, person2: Person) =
24+
person1.Equals(person2)
25+
26+
static member (!=) (person1: Person, person2: Person) =
27+
not (person1.Equals(person2))
5328

5429
// <Snippet12>
5530
// Create a Person object for each job applicant.

0 commit comments

Comments
 (0)