diff --git a/snippets/csharp/System/IEquatableT/Equals/Equals.csproj b/snippets/csharp/System/IEquatableT/Equals/Equals.csproj
new file mode 100644
index 00000000000..2150e3797ba
--- /dev/null
+++ b/snippets/csharp/System/IEquatableT/Equals/Equals.csproj
@@ -0,0 +1,10 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
diff --git a/snippets/csharp/System/IEquatableT/Equals/EqualsEx1.cs b/snippets/csharp/System/IEquatableT/Equals/EqualsEx1.cs
deleted file mode 100644
index 66a01f732b9..00000000000
--- a/snippets/csharp/System/IEquatableT/Equals/EqualsEx1.cs
+++ /dev/null
@@ -1,137 +0,0 @@
-//
-using System;
-using System.Collections.Generic;
-using System.Text.RegularExpressions;
-
-public class Person : IEquatable
-{
- private string uniqueSsn;
- private string lName;
-
- public Person(string lastName, string ssn)
- {
- this.SSN = ssn;
- this.LastName = lastName;
- }
-
- public string SSN
- {
- get { return this.uniqueSsn; }
- set {
- if (Regex.IsMatch(value, @"\d{9}"))
- uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3),
- value.Substring(3, 2),
- value.Substring(5, 4));
- else if (Regex.IsMatch(value, @"\d{3}-\d{2}-\d{4}"))
- uniqueSsn = value;
- else
- throw new FormatException("The social security number has an invalid format.");
- }
- }
-
- public string LastName
- {
- get { return this.lName; }
- set {
- if (String.IsNullOrEmpty(value))
- throw new ArgumentException("The last name cannot be null or empty.");
- else
- this.lName = value;
- }
- }
-
- public bool Equals(Person other)
- {
- if (other == null)
- return false;
-
- if (this.uniqueSsn == other.uniqueSsn)
- return true;
- else
- return false;
- }
-
- public override bool Equals(Object obj)
- {
- if (obj == null)
- return false;
-
- Person personObj = obj as Person;
- if (personObj == null)
- return false;
- else
- return Equals(personObj);
- }
-
- public override int GetHashCode()
- {
- return this.SSN.GetHashCode();
- }
-
- public static bool operator == (Person person1, Person person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return Object.Equals(person1, person2);
-
- return person1.Equals(person2);
- }
-
- public static bool operator != (Person person1, Person person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return ! Object.Equals(person1, person2);
-
- return ! (person1.Equals(person2));
- }
-}
-//
-
-// Snippet2>
-public class TestIEquatable
-{
- public static void Main()
- {
- // Create a Person object for each job applicant.
- Person applicant1 = new Person("Jones", "099-29-4999");
- Person applicant2 = new Person("Jones", "199-29-3999");
- Person applicant3 = new Person("Jones", "299-49-6999");
-
- // Add applicants to a List object.
- List applicants = new List();
- applicants.Add(applicant1);
- applicants.Add(applicant2);
- applicants.Add(applicant3);
-
- // Create a Person object for the final candidate.
- Person candidate = new Person("Jones", "199-29-3999");
- if (applicants.Contains(candidate))
- Console.WriteLine("Found {0} (SSN {1}).",
- candidate.LastName, candidate.SSN);
- else
- Console.WriteLine("Applicant {0} not found.", candidate.SSN);
-
- // Call the shared inherited Equals(Object, Object) method.
- // It will in turn call the IEquatable(Of T).Equals implementation.
- Console.WriteLine("{0}({1}) already on file: {2}.",
- applicant2.LastName,
- applicant2.SSN,
- Person.Equals(applicant2, candidate));
- }
-}
-// The example displays the following output:
-// Found Jones (SSN 199-29-3999).
-// Jones(199-29-3999) already on file: True.
-//
-
-// This tests the handling of null values, but does not appear in the documentation.
-//
-//public class Example
-//{
-// public static void Main()
-// {
-// var p1 = new Person("Joe", "613-24-0068");
-// Person p2 = null;
-//
-// Console.WriteLine(p1 == p2);
-// }
-//}
diff --git a/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs b/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs
deleted file mode 100644
index b7e12b95342..00000000000
--- a/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs
+++ /dev/null
@@ -1,131 +0,0 @@
-//
-using System;
-using System.Collections.Generic;
-using System.Text.RegularExpressions;
-
-public class Person : IEquatable
-{
- private string uniqueSsn;
- private string lName;
-
- public Person(string lastName, string ssn)
- {
- if (Regex.IsMatch(ssn, @"\d{9}"))
- uniqueSsn = $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}";
- else if (Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}"))
- uniqueSsn = ssn;
- else
- throw new FormatException("The social security number has an invalid format.");
-
- this.LastName = lastName;
- }
-
- public string SSN
- {
- get { return this.uniqueSsn; }
- }
-
- public string LastName
- {
- get { return this.lName; }
- set {
- if (String.IsNullOrEmpty(value))
- throw new ArgumentException("The last name cannot be null or empty.");
- else
- this.lName = value;
- }
- }
-
- public bool Equals(Person other)
- {
- if (other == null)
- return false;
-
- if (this.uniqueSsn == other.uniqueSsn)
- return true;
- else
- return false;
- }
-
- public override bool Equals(Object obj)
- {
- if (obj == null)
- return false;
-
- Person personObj = obj as Person;
- if (personObj == null)
- return false;
- else
- return Equals(personObj);
- }
-
- public override int GetHashCode()
- {
- return this.SSN.GetHashCode();
- }
-
- public static bool operator == (Person person1, Person person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return Object.Equals(person1, person2);
-
- return person1.Equals(person2);
- }
-
- public static bool operator != (Person person1, Person person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return ! Object.Equals(person1, person2);
-
- return ! (person1.Equals(person2));
- }
-}
-//
-
-public class TestIEquatable
-{
- public static void Main()
- {
- // Create a Person object for each job applicant.
- Person applicant1 = new Person("Jones", "099-29-4999");
- Person applicant2 = new Person("Jones", "199-29-3999");
- Person applicant3 = new Person("Jones", "299-49-6999");
-
- // Add applicants to a List object.
- List applicants = new List();
- applicants.Add(applicant1);
- applicants.Add(applicant2);
- applicants.Add(applicant3);
-
- // Create a Person object for the final candidate.
- Person candidate = new Person("Jones", "199-29-3999");
- if (applicants.Contains(candidate))
- Console.WriteLine("Found {0} (SSN {1}).",
- candidate.LastName, candidate.SSN);
- else
- Console.WriteLine("Applicant {0} not found.", candidate.SSN);
-
- // Call the shared inherited Equals(Object, Object) method.
- // It will in turn call the IEquatable(Of T).Equals implementation.
- Console.WriteLine("{0}({1}) already on file: {2}.",
- applicant2.LastName,
- applicant2.SSN,
- Person.Equals(applicant2, candidate));
- }
-}
-// The example displays the following output:
-// Found Jones (SSN 199-29-3999).
-// Jones(199-29-3999) already on file: True.
-
-// This tests the handling of null values, but does not appear in the documentation.
-//
-//public class Example
-//{
-// public static void Main()
-// {
-// var p1 = new Person("Joe", "613-24-0068");
-// Person p2 = null;
-//
-// Console.WriteLine(p1 == p2);
-// }
-//}
diff --git a/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs b/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs
new file mode 100644
index 00000000000..e5e6a43bac0
--- /dev/null
+++ b/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs
@@ -0,0 +1,56 @@
+//
+List applicants = new List()
+{
+ new Person("Jones", "099-29-4999"),
+ new Person("Jones", "199-29-3999"),
+ new Person("Jones", "299-49-6999")
+};
+
+// Create a Person object for the final candidate.
+Person candidate = new Person("Jones", "199-29-3999");
+bool contains = applicants.Contains(candidate);
+Console.WriteLine($"{candidate.LastName} ({candidate.NationalId}) is on record: {contains}");
+// The example prints the following output:
+// Jones (199-29-3999) is on record: True
+//
+
+//
+public class Person : IEquatable
+{
+ public Person(string lastName, string ssn)
+ {
+ LastName = lastName;
+ NationalId = ssn;
+ }
+
+ public string LastName { get; }
+
+ public string NationalId { get; }
+
+ public bool Equals(Person? other) => other is not null && other.NationalId == NationalId;
+
+ public override bool Equals(object? obj) => Equals(obj as Person);
+
+ public override int GetHashCode() => NationalId.GetHashCode();
+
+ public static bool operator ==(Person person1, Person person2)
+ {
+ if (person1 is null)
+ {
+ return person2 is null;
+ }
+
+ return person1.Equals(person2);
+ }
+
+ public static bool operator !=(Person person1, Person person2)
+ {
+ if (person1 is null)
+ {
+ return person2 is not null;
+ }
+
+ return !person1.Equals(person2);
+ }
+}
+//
diff --git a/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs b/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs
deleted file mode 100644
index cfb5e526740..00000000000
--- a/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs
+++ /dev/null
@@ -1,135 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Text.RegularExpressions;
-
-public class Person : IEquatable
-{
- private string uniqueSsn;
- private string lName;
-
- public Person(string lastName, string ssn)
- {
- this.SSN = ssn;
- this.LastName = lastName;
- }
-
- public string SSN
- {
- get { return this.uniqueSsn; }
- set {
- if (Regex.IsMatch(value, @"\d{9}"))
- uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3),
- value.Substring(3, 2),
- value.Substring(5, 4));
- else if (Regex.IsMatch(value, @"\d{3}-\d{2}-\d{4}"))
- uniqueSsn = value;
- else
- throw new FormatException("The social security number has an invalid format.");
- }
- }
-
- public string LastName
- {
- get { return this.lName; }
- set {
- if (String.IsNullOrEmpty(value))
- throw new ArgumentException("The last name cannot be null or empty.");
- else
- this.lName = value;
- }
- }
-
- public bool Equals(Person other)
- {
- if (other == null)
- return false;
-
- if (this.uniqueSsn == other.uniqueSsn)
- return true;
- else
- return false;
- }
-
- public override bool Equals(Object obj)
- {
- if (obj == null)
- return false;
-
- Person personObj = obj as Person;
- if (personObj == null)
- return false;
- else
- return Equals(personObj);
- }
-
- public override int GetHashCode()
- {
- return this.SSN.GetHashCode();
- }
-
- public static bool operator == (Person person1, Person person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return Object.Equals(person1, person2);
-
- return person1.Equals(person2);
- }
-
- public static bool operator != (Person person1, Person person2)
- {
- if (((object)person1) == null || ((object)person2) == null)
- return ! Object.Equals(person1, person2);
-
- return ! (person1.Equals(person2));
- }
-}
-
-//
-public class TestIEquatable
-{
- public static void Main()
- {
- // Create a Person object for each job applicant.
- Person applicant1 = new Person("Jones", "099-29-4999");
- Person applicant2 = new Person("Jones", "199-29-3999");
- Person applicant3 = new Person("Jones", "299-49-6999");
-
- // Add applicants to a List object.
- List applicants = new List();
- applicants.Add(applicant1);
- applicants.Add(applicant2);
- applicants.Add(applicant3);
-
- // Create a Person object for the final candidate.
- Person candidate = new Person("Jones", "199-29-3999");
- if (applicants.Contains(candidate))
- Console.WriteLine("Found {0} (SSN {1}).",
- candidate.LastName, candidate.SSN);
- else
- Console.WriteLine("Applicant {0} not found.", candidate.SSN);
-
- // Call the shared inherited Equals(Object, Object) method.
- // It will in turn call the IEquatable(Of T).Equals implementation.
- Console.WriteLine("{0}({1}) already on file: {2}.",
- applicant2.LastName,
- applicant2.SSN,
- Person.Equals(applicant2, candidate));
- }
-}
-// The example displays the following output:
-// Found Jones (SSN 199-29-3999).
-// Jones(199-29-3999) already on file: True.
-//
-
-// This tests the handling of null values, but does not appear in the documentation.
-//
-//public class Example
-//{
-// public static void Main()
-// {
-// var p1 = new Person("Joe", "613-24-0068");
-// Person p2 = null;
-//
-// Console.WriteLine(p1 == p2);
-// }
-//}
diff --git a/snippets/fsharp/System/IEquatableT/Equals/Equals.fsproj b/snippets/fsharp/System/IEquatableT/Equals/Equals.fsproj
new file mode 100644
index 00000000000..32c2318f221
--- /dev/null
+++ b/snippets/fsharp/System/IEquatableT/Equals/Equals.fsproj
@@ -0,0 +1,12 @@
+
+
+
+ Exe
+ net8.0
+
+
+
+
+
+
+
diff --git a/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs b/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs
deleted file mode 100644
index 29e42d75a6a..00000000000
--- a/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs
+++ /dev/null
@@ -1,93 +0,0 @@
-module EqualsEx2
-
-//
-open System
-open System.Text.RegularExpressions
-
-type Person(lastName, ssn) =
- let mutable lastName = lastName
- let ssn =
- if Regex.IsMatch(ssn, @"\d{9}") then
- $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}"
- elif Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}") then
- ssn
- else
- raise (FormatException "The social security number has an invalid format.")
-
- member _.SSN =
- ssn
-
- member _.LastName
- with get () = lastName
- and set (value) =
- if String.IsNullOrEmpty value then
- invalidArg (nameof value) "The last name cannot be null or empty."
- else
- lastName <- value
-
- static member op_Equality (person1: Person, person2: Person) =
- if box person1 |> isNull || box person2 |> isNull then
- Object.Equals(person1, person2)
- else
- person1.Equals person2
-
- static member op_Inequality (person1: Person, person2: Person) =
- if box person1 |> isNull || box person2 |> isNull then
- Object.Equals(person1, person2) |> not
- else
- person1.Equals person2 |> not
-
- override _.GetHashCode() =
- ssn.GetHashCode()
-
- override this.Equals(obj: obj) =
- match obj with
- | :? Person as personObj ->
- (this :> IEquatable<_>).Equals personObj
- | _ -> false
-
- interface IEquatable with
- member this.Equals(other: Person) =
- match box other with
- | null -> false
- | _ ->
- this.SSN = other.SSN
-//
-// Create a Person object for each job applicant.
-let applicant1 = Person("Jones", "099-29-4999")
-let applicant2 = Person("Jones", "199-29-3999")
-let applicant3 = Person("Jones", "299-49-6999")
-
-// Add applicants to a List object.
-let applicants = ResizeArray()
-applicants.Add applicant1
-applicants.Add applicant2
-applicants.Add applicant3
-
-// Create a Person object for the final candidate.
-let candidate = Person("Jones", "199-29-3999")
-if applicants.Contains candidate then
- printfn $"Found {candidate.LastName} (SSN {candidate.SSN})."
-else
- printfn $"Applicant {candidate.SSN} not found."
-
-// Call the shared inherited Equals(Object, Object) method.
-// It will in turn call the IEquatable.Equals implementation.
-printfn $"{applicant2.LastName}({applicant2.SSN}) already on file: {Person.Equals(applicant2, candidate)}."
-
-// The example displays the following output:
-// Found Jones (SSN 199-29-3999).
-// Jones(199-29-3999) already on file: True.
-
-// This tests the handling of null values, but does not appear in the documentation.
-//
-//public class Example
-//{
-// public static void Main()
-// {
-// var p1 = new Person("Joe", "613-24-0068")
-// Person p2 = null
-//
-// Console.WriteLine(p1 == p2)
-// }
-//}
\ No newline at end of file
diff --git a/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs b/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs
new file mode 100644
index 00000000000..9aff81cdf57
--- /dev/null
+++ b/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs
@@ -0,0 +1,39 @@
+
+//
+open System
+
+type Person(lastName: string, nationalId: string) =
+ member this.LastName = lastName
+ member this.NationalId = nationalId
+
+ interface IEquatable with
+ member this.Equals(other: Person) =
+ other.NationalId = this.NationalId
+
+ override this.Equals(obj: obj) =
+ match obj with
+ | :? Person as person -> (this :> IEquatable).Equals(person)
+ | _ -> false
+
+ override this.GetHashCode() =
+ this.NationalId.GetHashCode()
+
+ static member (==) (person1: Person, person2: Person) =
+ person1.Equals(person2)
+
+ static member (!=) (person1: Person, person2: Person) =
+ not (person1.Equals(person2))
+//
+
+//
+let applicants =
+ [ Person("Jones", "099-29-4999")
+ Person("Jones", "199-29-3999")
+ Person("Jones", "299-49-6999") ]
+
+let candidate = Person("Jones", "199-29-3999")
+let contains = List.contains candidate applicants
+printfn "%s (%s) is on record: %b" candidate.LastName candidate.NationalId contains
+// The example prints the following output:
+// Jones (199-29-3999) is on record: true
+//
diff --git a/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs b/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs
deleted file mode 100644
index 39e48b695c9..00000000000
--- a/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs
+++ /dev/null
@@ -1,80 +0,0 @@
-open System
-open System.Collections.Generic
-open System.Text.RegularExpressions
-
-type Person(lastName, ssn) =
- let mutable lastName = lastName
- let ssn =
- if Regex.IsMatch(ssn, @"\d{9}") then
- $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}"
- elif Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}") then
- ssn
- else
- raise (FormatException "The social security number has an invalid format.")
-
- member _.SSN =
- ssn
-
- member _.LastName
- with get () = lastName
- and set (value) =
- if String.IsNullOrEmpty value then
- invalidArg (nameof value) "The last name cannot be null or empty."
- else
- lastName <- value
-
- static member op_Equality (person1: Person, person2: Person) =
- if box person1 |> isNull || box person2 |> isNull then
- Object.Equals(person1, person2)
- else
- person1.Equals person2
-
- static member op_Inequality (person1: Person, person2: Person) =
- if box person1 |> isNull || box person2 |> isNull then
- Object.Equals(person1, person2) |> not
- else
- person1.Equals person2 |> not
-
- override _.GetHashCode() =
- ssn.GetHashCode()
-
- override this.Equals(obj: obj) =
- match obj with
- | :? Person as personObj ->
- (this :> IEquatable<_>).Equals personObj
- | _ -> false
-
- interface IEquatable with
- member this.Equals(other: Person) =
- match box other with
- | null -> false
- | _ ->
- this.SSN = other.SSN
-
-//
-// Create a Person object for each job applicant.
-let applicant1 = Person("Jones", "099-29-4999")
-let applicant2 = Person("Jones", "199-29-3999")
-let applicant3 = Person("Jones", "299-49-6999")
-
-// Add applicants to a List object.
-let applicants = ResizeArray()
-applicants.Add applicant1
-applicants.Add applicant2
-applicants.Add applicant3
-
-// Create a Person object for the final candidate.
-let candidate = Person("Jones", "199-29-3999")
-if applicants.Contains candidate then
- printfn $"Found {candidate.LastName} (SSN {candidate.SSN})."
-else
- printfn $"Applicant {candidate.SSN} not found."
-
-// Call the shared inherited Equals(Object, Object) method.
-// It will in turn call the IEquatable.Equals implementation.
-printfn $"{applicant2.LastName}({applicant2.SSN}) already on file: {Person.Equals(applicant2, candidate)}."
-
-// The example displays the following output:
-// Found Jones (SSN 199-29-3999).
-// Jones(199-29-3999) already on file: True.
-//
\ No newline at end of file
diff --git a/snippets/fsharp/System/IEquatableT/Equals/fs.fsproj b/snippets/fsharp/System/IEquatableT/Equals/fs.fsproj
deleted file mode 100644
index 34dea6b9fa0..00000000000
--- a/snippets/fsharp/System/IEquatableT/Equals/fs.fsproj
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
- Exe
- net6.0
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Equals.vbproj b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Equals.vbproj
new file mode 100644
index 00000000000..4855707fd9d
--- /dev/null
+++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Equals.vbproj
@@ -0,0 +1,9 @@
+
+
+
+ Exe
+ vb
+ net8.0
+
+
+
diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb
deleted file mode 100644
index 425406deef9..00000000000
--- a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb
+++ /dev/null
@@ -1,120 +0,0 @@
-' Visual Basic .NET Document
-Option Strict On
-
-'
-Imports System.Collections.Generic
-Imports System.Text.RegularExpressions
-
-Public Class Person : Implements IEquatable(Of Person)
- Private uniqueSsn As String
- Private lName As String
-
- Public Sub New(lastName As String, ssn As String)
- If Regex.IsMatch(ssn, "\d{9}") Then
- uniqueSsn = $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}"
- ElseIf Regex.IsMatch(ssn, "\d{3}-\d{2}-\d{4}") Then
- uniqueSsn = ssn
- Else
- Throw New FormatException("The social security number has an invalid format.")
- End If
- Me.LastName = lastName
- End Sub
-
- Public ReadOnly Property SSN As String
- Get
- Return Me.uniqueSsn
- End Get
- End Property
-
- Public Property LastName As String
- Get
- Return Me.lName
- End Get
- Set
- If String.IsNullOrEmpty(value) Then
- Throw New ArgumentException("The last name cannot be null or empty.")
- Else
- lname = value
- End If
- End Set
- End Property
-
- Public Overloads Function Equals(other As Person) As Boolean _
- Implements IEquatable(Of Person).Equals
- If other Is Nothing Then Return False
-
- If Me.uniqueSsn = other.uniqueSsn Then
- Return True
- Else
- Return False
- End If
- End Function
-
- Public Overrides Function Equals(obj As Object) As Boolean
- If obj Is Nothing Then Return False
-
- Dim personObj As Person = TryCast(obj, Person)
- If personObj Is Nothing Then
- Return False
- Else
- Return Equals(personObj)
- End If
- End Function
-
- Public Overrides Function GetHashCode() As Integer
- Return Me.SSN.GetHashCode()
- End Function
-
- Public Shared Operator = (person1 As Person, person2 As Person) As Boolean
- If person1 Is Nothing OrElse person2 Is Nothing Then
- Return Object.Equals(person1, person2)
- End If
-
- Return person1.Equals(person2)
- End Operator
-
- Public Shared Operator <> (person1 As Person, person2 As Person) As Boolean
- If person1 Is Nothing OrElse person2 Is Nothing Then
- Return Not Object.Equals(person1, person2)
- End If
-
- Return Not person1.Equals(person2)
- End Operator
-End Class
-'
-
-Module TestIEquatable
- Public Sub Main()
- ' Create a Person object for each job applicant.
- Dim applicant1 As New Person("Jones", "099-29-4999")
- Dim applicant2 As New Person("Jones", "199-29-3999")
- Dim applicant3 As New Person("Jones", "299-49-6999")
-
- ' Add applicants to a List object.
- Dim applicants As New List(Of Person)
- applicants.Add(applicant1)
- applicants.Add(applicant2)
- applicants.Add(applicant3)
-
- ' Create a Person object for the final candidate.
- Dim candidate As New Person("Jones", "199-29-3999")
-
- If applicants.Contains(candidate) Then
- Console.WriteLine("Found {0} (SSN {1}).", _
- candidate.LastName, candidate.SSN)
- Else
- Console.WriteLine("Applicant {0} not found.", candidate.SSN)
- End If
-
- ' Call the shared inherited Equals(Object, Object) method.
- ' It will in turn call the IEquatable(Of T).Equals implementation.
- Console.WriteLine("{0}({1}) already on file: {2}.", _
- applicant2.LastName, _
- applicant2.SSN, _
- Person.Equals(applicant2, candidate))
- End Sub
-End Module
-' The example displays the following output:
-' Found Jones (SSN 199-29-3999).
-' Jones(199-29-3999) already on file: True.
-
diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb
new file mode 100644
index 00000000000..73cb24e18b8
--- /dev/null
+++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb
@@ -0,0 +1,61 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Public Class Person
+ Implements IEquatable(Of Person)
+
+ Public Sub New(lastName As String, nationalId As String)
+ Me.LastName = lastName
+ Me.NationalId = nationalId
+ End Sub
+
+ Public ReadOnly Property LastName As String
+ Public ReadOnly Property NationalId As String
+
+ Public Overloads Function Equals(other As Person) As Boolean Implements IEquatable(Of Person).Equals
+ Return other IsNot Nothing AndAlso other.NationalId = Me.NationalId
+ End Function
+
+ Public Overrides Function Equals(obj As Object) As Boolean
+ Return Equals(TryCast(obj, Person))
+ End Function
+
+ Public Overrides Function GetHashCode() As Integer
+ Return NationalId.GetHashCode()
+ End Function
+
+ Public Shared Operator =(person1 As Person, person2 As Person) As Boolean
+ If person1 Is Nothing Then
+ Return person2 Is Nothing
+ End If
+ Return person1.Equals(person2)
+ End Operator
+
+ Public Shared Operator <>(person1 As Person, person2 As Person) As Boolean
+ If person1 Is Nothing Then
+ Return person2 IsNot Nothing
+ End If
+ Return Not person1.Equals(person2)
+ End Operator
+End Class
+'
+
+Module EqualsExample
+ Public Sub Main()
+ '
+ Dim applicants As New List(Of Person)
+ applicants.Add(New Person("Jones", "099-29-4999"))
+ applicants.Add(New Person("Jones", "199-29-3999"))
+ applicants.Add(New Person("Jones", "299-49-6999"))
+
+ ' Create a Person object for the final candidate.
+ Dim candidate As New Person("Jones", "199-29-3999")
+ Dim contains As Boolean = applicants.Contains(candidate)
+ Console.WriteLine($"{candidate.LastName} ({candidate.NationalId}) is on record: {contains}")
+ ' The example prints the following output:
+ ' Jones (199-29-3999) Is on record True
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb
deleted file mode 100644
index 6de5888d74a..00000000000
--- a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb
+++ /dev/null
@@ -1,125 +0,0 @@
-' Visual Basic .NET Document
-Option Strict On
-
-Imports System.Collections.Generic
-Imports System.Text.RegularExpressions
-
-Public Class Person : Implements IEquatable(Of Person)
- Private uniqueSsn As String
- Private lName As String
-
- Public Sub New(lastName As String, ssn As String)
- Me.SSN = ssn
- Me.LastName = lastName
- End Sub
-
- Public Property SSN As String
- Set
- If Regex.IsMatch(value, "\d{9}") Then
- uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3), _
- value.Substring(3, 2), _
- value.Substring(5, 4))
- ElseIf Regex.IsMatch(value, "\d{3}-\d{2}-\d{4}") Then
- uniqueSsn = value
- Else
- Throw New FormatException("The social security number has an invalid format.")
- End If
- End Set
- Get
- Return Me.uniqueSsn
- End Get
- End Property
-
- Public Property LastName As String
- Get
- Return Me.lName
- End Get
- Set
- If String.IsNullOrEmpty(value) Then
- Throw New ArgumentException("The last name cannot be null or empty.")
- Else
- lname = value
- End If
- End Set
- End Property
-
- Public Overloads Function Equals(other As Person) As Boolean _
- Implements IEquatable(Of Person).Equals
- If other Is Nothing Then Return False
-
- If Me.uniqueSsn = other.uniqueSsn Then
- Return True
- Else
- Return False
- End If
- End Function
-
- Public Overrides Function Equals(obj As Object) As Boolean
- If obj Is Nothing Then Return False
-
- Dim personObj As Person = TryCast(obj, Person)
- If personObj Is Nothing Then
- Return False
- Else
- Return Equals(personObj)
- End If
- End Function
-
- Public Overrides Function GetHashCode() As Integer
- Return Me.SSN.GetHashCode()
- End Function
-
- Public Shared Operator = (person1 As Person, person2 As Person) As Boolean
- If person1 Is Nothing OrElse person2 Is Nothing Then
- Return Object.Equals(person1, person2)
- End If
-
- Return person1.Equals(person2)
- End Operator
-
- Public Shared Operator <> (person1 As Person, person2 As Person) As Boolean
- If person1 Is Nothing OrElse person2 Is Nothing Then
- Return Not Object.Equals(person1, person2)
- End If
-
- Return Not person1.Equals(person2)
- End Operator
-End Class
-
-'
-Module TestIEquatable
- Public Sub Main()
- ' Create a Person object for each job applicant.
- Dim applicant1 As New Person("Jones", "099-29-4999")
- Dim applicant2 As New Person("Jones", "199-29-3999")
- Dim applicant3 As New Person("Jones", "299-49-6999")
-
- ' Add applicants to a List object.
- Dim applicants As New List(Of Person)
- applicants.Add(applicant1)
- applicants.Add(applicant2)
- applicants.Add(applicant3)
-
- ' Create a Person object for the final candidate.
- Dim candidate As New Person("Jones", "199-29-3999")
-
- If applicants.Contains(candidate) Then
- Console.WriteLine("Found {0} (SSN {1}).", _
- candidate.LastName, candidate.SSN)
- Else
- Console.WriteLine("Applicant {0} not found.", candidate.SSN)
- End If
-
- ' Call the shared inherited Equals(Object, Object) method.
- ' It will in turn call the IEquatable(Of T).Equals implementation.
- Console.WriteLine("{0}({1}) already on file: {2}.", _
- applicant2.LastName, _
- applicant2.SSN, _
- Person.Equals(applicant2, candidate))
- End Sub
-End Module
-' The example displays the following output:
-' Found Jones (SSN 199-29-3999).
-' Jones(199-29-3999) already on file: True.
-'
-
diff --git a/xml/System/IEquatable`1.xml b/xml/System/IEquatable`1.xml
index b30e47c72cc..47967e257e3 100644
--- a/xml/System/IEquatable`1.xml
+++ b/xml/System/IEquatable`1.xml
@@ -60,11 +60,16 @@
> The interface defines the method, which determines the sort order of instances of the implementing type. The interface defines the method, which determines the equality of instances of the implementing type.
The interface is used by generic collection objects such as , , and when testing for equality in such methods as `Contains`, `IndexOf`, `LastIndexOf`, and `Remove`. It should be implemented for any object that might be stored in a generic collection.
-
-
-
+
+For more information about implementing the interface, see remarks on the method.
+
## Examples
- See the example for the method.
+ The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `NationalId`. `NationalId` is considered to be a unique identifier, therefore the method returns `True` if the `NationalId` property of two `Person` objects is identical; otherwise, it returns `False`.
+ (Note that the F# example does not handle `null` values for `Person` instances.)
+
+ :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="Person":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="Person":::
+ :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb" id="Person":::
]]>
@@ -163,17 +168,18 @@
## Examples
- The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `SSN`. The method returns `True` if the `SSN` property of two `Person` objects is identical; otherwise, it returns `False`.
+ The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `NationalId`. `NationalId` is considered to be a unique identifier, therefore the method returns `True` if the `NationalId` property of two `Person` objects is identical; otherwise, it returns `False`.
+ (Note that the F# example does not handle `null` values for `Person` instances.)
- :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs" id="Snippet3":::
- :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs" id="Snippet3":::
- :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb" id="Snippet3":::
+ :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="Person":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="Person":::
+ :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb" id="Person":::
- `Person` objects can then be stored in a object and can be identified by the `Contains` method, as the following example shows.
+ When a `Person` is stored in a , `Contains` uses its implementation to search for a match.
- :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs" id="Snippet12":::
- :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs" id="Snippet12":::
- :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb" id="Snippet12":::
+ :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="PersonSample":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="PersonSample":::
+ :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb" id="PersonSample":::
]]>